Post to twitter php oauth


#1

how to post twitter with php this example code

    <?php
    ob_start();
    require("twitter/twitteroauth.php");
    require 'config/twconfig.php';
    session_start();
    
    $twitteroauth = new TwitterOAuth(YOUR_CONSUMER_KEY, YOUR_CONSUMER_SECRET);
    // Requesting authentication tokens, the parameter is the URL we will be redirected to
    $request_token = $twitteroauth->getRequestToken();
    
    // Saving them into the session
    
    $_SESSION['oauth_token'] = $request_token['oauth_token'];
    $_SESSION['oauth_token_secret'] = $request_token['oauth_token_secret'];
    // If everything goes well..
    if ($twitteroauth->http_code == 200) {
        // Let's generate the URL and redirect
        $url = $twitteroauth->getAuthorizeURL($request_token['oauth_token']);
        // We've got everything we need
        $twitteroauth = new TwitterOAuth(YOUR_CONSUMER_KEY, YOUR_CONSUMER_SECRET, $_SESSION['oauth_token'], $_SESSION['oauth_token_secret']);
    // Let's request the access token
        $access_token = $twitteroauth->getAccessToken($_GET['oauth_verifier']);
    // Save it in a session var
        $_SESSION['access_token'] = $access_token;
    // Let's get the user's info
        $user_info = $twitteroauth->get('account/verify_credentials');
        $tweet = $_POST['name'];
        $twitteroauth->post('statuses/update', array('status' => $tweet ));
    
        header('Location: ' . $url);
    } else {
        // It's a bad idea to kill the script, but we've got to know when there's an error.
        die('Something wrong happened.');
    }
    ?>

#2

If you are using a pre existing library to access the Twitter API, then the github pages have plenty of examples and step by step instructions on how to use them.


#3

yes I 've tried one by one but could not you could give an example


#4

Well the code you have is only one page - follow the instructions on the github page and take some time to understand what each class is doing and you can’t go wrong.