PHP & Twitter (API)

Discussion in 'PHP' started by Jenncria WebMaster, Sep 3, 2010.

  1. #1
    Hi at all DP,

    I need help to solve a problem with new Twitter's API interface... this is the question:

    How can I send/tweet a simple message like "hello world" to my twitter account?

    Thanks
     
    Jenncria WebMaster, Sep 3, 2010 IP
  2. Jenncria WebMaster

    Jenncria WebMaster Member

    Messages:
    200
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    38
    #2
    I found a solution by myself! :)
     
    Jenncria WebMaster, Sep 3, 2010 IP
  3. wwfc_barmy_army

    wwfc_barmy_army Well-Known Member

    Messages:
    122
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    110
    #3
    In case anyone sees this and wants to know how, here is code that we have recently used:

    twitterAPI.php
    <?php
    
    // A simple function using Curl to post (GET) to Twitter
    
    function postToTwitter($username,$password,$message){
    
        $host = "http://twitter.com/statuses/update.xml?status=".urlencode(stripslashes(urldecode($message)));
    
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $host);
        curl_setopt($ch, CURLOPT_VERBOSE, 1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
        curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
        curl_setopt($ch, CURLOPT_POST, 1);
    
        $result = curl_exec($ch);
        // Look at the returned header
        $resultArray = curl_getinfo($ch);
    
        curl_close($ch);
    
        if($resultArray['http_code'] == "200"){
             $twitter_status='Your message has been sent! <a href="http://twitter.com/'.$username.'">See your profile</a>';
        } else {
             $twitter_status="Error posting to Twitter. Retry";
        }
    	return $twitter_status;
    }
    ?>
    PHP:
    other.php
    <?php
    $twitter_message = "Hello World!";
    /* ---------------------------------------- */
    // Change these parameters with your Twitter
    // user name and Twitter password.
    /* ---------------------------------------- */
    $twitter_username ='USERNAME';
    $twitter_psw ='PASSWORD';
    /* ---------------------------------------- */
    
    /* Don't change the code belove
    /* ---------------------------------------- */
    require('twitterAPI.php');
    if(strlen($twitter_message)<1){ $error=1; } else { $twitter_status=postToTwitter($twitter_username, $twitter_psw, $twitter_message); echo "</br>Twitter Has been Updated!</br></div>"; }
    /* ---------------------------------------- */
    ?>
    
    PHP:
     
    wwfc_barmy_army, Sep 4, 2010 IP
  4. Jenncria WebMaster

    Jenncria WebMaster Member

    Messages:
    200
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    38
    #4
    Hi wwfc_barmy_army,

    Thanks for your help, but I have to say that your code doesn't work anymore. Twitter API are changed few days ago and now it is use token. So now you don't need anymore user name and password but api key, consumer secret, out token and token secret.

    However I used a new version of Tijs Verkoyen and now I can tweet again.
     
    Jenncria WebMaster, Sep 5, 2010 IP
  5. Rainulf

    Rainulf Active Member

    Messages:
    373
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    85
    #5
    lol yeah, the basic auth has been deprecated last week. Too bad I can't fish passwords anymore.. =( (jks)

    I use abraham's twitteroauth library, you might also wanna check that one out. :) I find it very easy to use
     
    Rainulf, Sep 5, 2010 IP