Simplest PHP example for retrieving user_timeline with Twitter API version 1.1

前端 未结 14 2311
盖世英雄少女心
盖世英雄少女心 2020-11-21 17:37

Because of the Twitter API 1.0 retirement as of June 11th 2013, the script below does not work anymore.

// Create curl resource 
$ch = curl_init(); 
// Set u         


        
相关标签:
14条回答
  • 2020-11-21 17:57

    Here's a brief one for getting a specified number of tweets from your timeline. It basically does the same thing as the other examples, only with less code.

    Just fill in the keys and adjust $count to your liking:

    $url = 'https://api.twitter.com/1.1/statuses/user_timeline.json';
    $count = '10';
    
    $oauth = array('count' => $count,
                   'oauth_consumer_key' => '[CONSUMER KEY]',
                   'oauth_nonce' => md5(mt_rand()),
                   'oauth_signature_method' => 'HMAC-SHA1',
                   'oauth_timestamp' => time(),
                   'oauth_token' => '[ACCESS TOKEN]',
                   'oauth_version' => '1.0');
    
    $oauth['oauth_signature'] = base64_encode(hash_hmac('sha1', 'GET&' . rawurlencode($url) . '&' . rawurlencode(implode('&', array_map(function ($v, $k) { return $k . '=' . $v; }, $oauth, array_keys($oauth)))), '[CONSUMER SECRET]&[ACCESS TOKEN SECRET]', true));
    
    $twitterData = json_decode(file_get_contents($url . '?count=' . $count, false, stream_context_create(array('http' => array('method' => 'GET',
                                                                                                                               'header' => 'Authorization: OAuth ' 
                                                                                                                                           . implode(', ', array_map(function ($v, $k) { return $k . '="' . rawurlencode($v) . '"'; }, $oauth, array_keys($oauth))))))));
    

    This one uses anonymous functions and file_get_contents instead of the cURL library. Note the use of an MD5 hashed nonce. Everyone seems to be going along with the time() nonce, however, most examples on the web concerning OAuth use some kind of encrypted string (like this one: http://www.sitepoint.com/understanding-oauth-1/). This makes more sense to me too.

    Further note: you need PHP 5.3+ for the anonymous functions (in case your server/computer is in some cold war cave and you can't upgrade it).

    0 讨论(0)
  • 2020-11-21 18:00

    You'll need a to create an "app" on Twitter (and you need a Twitter account to do this).

    Then, you need to use OAuth to make an authorized request to Twitter.

    You can use the GET statuses/user_timeline resource to get a list of recent tweets.

    0 讨论(0)
  • 2020-11-21 18:02

    This question helped me a lot but didn't get me all the way in understanding what needs to happen. This blog post did an amazing job of walking me through it.

    Here are the important bits all in one place:

    • As pointed out above, you MUST sign your 1.1 API requests. If you are doing something like getting public statuses, you'll want an application key rather than a user key. The full link to the page you want is: https://dev.twitter.com/apps
    • You must hash ALL the parameters, both the oauth ones AND the get parameters (or POST parameters) together.
    • You must SORT the parameters before reducing them to the url encoded form that gets hashed.
    • You must encode some things multiple times - for example, you create a query string from the parameters' url-encoded values, and then you url encode THAT and concatenate with the method type and the url.

    I sympathize with all the headaches, so here's some code to wrap it all up:

    $token = 'YOUR TOKEN';
    $token_secret = 'TOKEN SECRET';
    $consumer_key = 'YOUR KEY';
    $consumer_secret = 'KEY SECRET';
    
    $host = 'api.twitter.com';
    $method = 'GET';
    $path = '/1.1/statuses/user_timeline.json'; // api call path
    
    $query = array( // query parameters
        'screen_name' => 'twitterapi',
        'count' => '2'
    );
    
    $oauth = array(
        'oauth_consumer_key' => $consumer_key,
        'oauth_token' => $token,
        'oauth_nonce' => (string)mt_rand(), // a stronger nonce is recommended
        'oauth_timestamp' => time(),
        'oauth_signature_method' => 'HMAC-SHA1',
        'oauth_version' => '1.0'
    );
    
    $oauth = array_map("rawurlencode", $oauth); // must be encoded before sorting
    $query = array_map("rawurlencode", $query);
    
    $arr = array_merge($oauth, $query); // combine the values THEN sort
    
    asort($arr); // secondary sort (value)
    ksort($arr); // primary sort (key)
    
    // http_build_query automatically encodes, but our parameters
    // are already encoded, and must be by this point, so we undo
    // the encoding step
    $querystring = urldecode(http_build_query($arr, '', '&'));
    
    $url = "https://$host$path";
    
    // mash everything together for the text to hash
    $base_string = $method."&".rawurlencode($url)."&".rawurlencode($querystring);
    
    // same with the key
    $key = rawurlencode($consumer_secret)."&".rawurlencode($token_secret);
    
    // generate the hash
    $signature = rawurlencode(base64_encode(hash_hmac('sha1', $base_string, $key, true)));
    
    // this time we're using a normal GET query, and we're only encoding the query params
    // (without the oauth params)
    $url .= "?".http_build_query($query);
    
    $oauth['oauth_signature'] = $signature; // don't want to abandon all that work!
    ksort($oauth); // probably not necessary, but twitter's demo does it
    
    // also not necessary, but twitter's demo does this too
    function add_quotes($str) { return '"'.$str.'"'; }
    $oauth = array_map("add_quotes", $oauth);
    
    // this is the full value of the Authorization line
    $auth = "OAuth " . urldecode(http_build_query($oauth, '', ', '));
    
    // if you're doing post, you need to skip the GET building above
    // and instead supply query parameters to CURLOPT_POSTFIELDS
    $options = array( CURLOPT_HTTPHEADER => array("Authorization: $auth"),
                      //CURLOPT_POSTFIELDS => $postfields,
                      CURLOPT_HEADER => false,
                      CURLOPT_URL => $url,
                      CURLOPT_RETURNTRANSFER => true,
                      CURLOPT_SSL_VERIFYPEER => false);
    
    // do our business
    $feed = curl_init();
    curl_setopt_array($feed, $options);
    $json = curl_exec($feed);
    curl_close($feed);
    
    $twitter_data = json_decode($json);
    
    0 讨论(0)
  • 2020-11-21 18:06

    Go to dev.twitter.com and create an application. This will provide you with the credentials you need. Here is an implementation I've recently written with PHP and cURL.

    <?php
        function buildBaseString($baseURI, $method, $params) {
            $r = array();
            ksort($params);
            foreach($params as $key=>$value){
                $r[] = "$key=" . rawurlencode($value);
            }
            return $method."&" . rawurlencode($baseURI) . '&' . rawurlencode(implode('&', $r));
        }
    
        function buildAuthorizationHeader($oauth) {
            $r = 'Authorization: OAuth ';
            $values = array();
            foreach($oauth as $key=>$value)
                $values[] = "$key=\"" . rawurlencode($value) . "\"";
            $r .= implode(', ', $values);
            return $r;
        }
    
        $url = "https://api.twitter.com/1.1/statuses/user_timeline.json";
    
        $oauth_access_token = "YOURVALUE";
        $oauth_access_token_secret = "YOURVALUE";
        $consumer_key = "YOURVALUE";
        $consumer_secret = "YOURVALUE";
    
        $oauth = array( 'oauth_consumer_key' => $consumer_key,
                        'oauth_nonce' => time(),
                        'oauth_signature_method' => 'HMAC-SHA1',
                        'oauth_token' => $oauth_access_token,
                        'oauth_timestamp' => time(),
                        'oauth_version' => '1.0');
    
        $base_info = buildBaseString($url, 'GET', $oauth);
        $composite_key = rawurlencode($consumer_secret) . '&' . rawurlencode($oauth_access_token_secret);
        $oauth_signature = base64_encode(hash_hmac('sha1', $base_info, $composite_key, true));
        $oauth['oauth_signature'] = $oauth_signature;
    
        // Make requests
        $header = array(buildAuthorizationHeader($oauth), 'Expect:');
        $options = array( CURLOPT_HTTPHEADER => $header,
                          //CURLOPT_POSTFIELDS => $postfields,
                          CURLOPT_HEADER => false,
                          CURLOPT_URL => $url,
                          CURLOPT_RETURNTRANSFER => true,
                          CURLOPT_SSL_VERIFYPEER => false);
    
        $feed = curl_init();
        curl_setopt_array($feed, $options);
        $json = curl_exec($feed);
        curl_close($feed);
    
        $twitter_data = json_decode($json);
    
    //print it out
    print_r ($twitter_data);
    
    ?>
    

    This can be run from the command line:

    $ php <name of PHP script>.php
    
    0 讨论(0)
  • 2020-11-21 18:06

    From their signature generator, you can generate curl commands of the form:

    curl --get 'https://api.twitter.com/1.1/statuses/user_timeline.json' --data 'count=2&screen_name=twitterapi' --header 'Authorization: OAuth oauth_consumer_key="YOUR_KEY", oauth_nonce="YOUR_NONCE", oauth_signature="YOUR-SIG", oauth_signature_method="HMAC-SHA1", oauth_timestamp="TIMESTAMP", oauth_token="YOUR-TOKEN", oauth_version="1.0"' --verbose
    
    0 讨论(0)
  • 2020-11-21 18:06

    If it is useful for anyone... In my blog I've implement the following PHP code in order to retrieve the last tweets, extract their most relevant data and then saved them into a MySQL database. It works because I got it in my blog.

    The "tweets" table where store them:

    CREATE TABLE IF NOT EXISTS `tweets` (
      `tweet_id` int(11) NOT NULL auto_increment,
      `id_tweet` bigint(20) NOT NULL,
      `text_tweet` char(144) NOT NULL,
      `datetime_tweet` datetime NOT NULL,
      `dayofweek_tweet` char(3) NOT NULL,
      `GMT_tweet` char(5) NOT NULL,
      `shorturl_tweet` char(23) NOT NULL,
      PRIMARY KEY  (`tweet_id`)
    ) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=83 ;
    

    get_tweets.php:

    <?php
    function buildBaseString($baseURI, $method, $params) {
        $r= array();
        ksort($params);
        foreach($params as $key=>$value){
            $r[]= "$key=".rawurlencode($value);
        }
        return $method."&".rawurlencode($baseURI).'&'.rawurlencode(implode('&', $r));
    }
    
    function buildAuthorizationHeader($oauth) {
        $r= 'Authorization: OAuth ';
        $values= array();
        foreach($oauth as $key=>$value) {
            $values[]= "$key=\"".rawurlencode($value)."\"";
        }
        $r.= implode(', ', $values);
        return $r;
    }
    
    function returnTweets($last_id) {
        $oauth_access_token         = "2687912757-vbyfJA483SEyj2HJ2K346aVMxtOIgVbsY4Edrsw";
        $oauth_access_token_secret  = "nIruzmR0bXqC3has4fTf8KAq4pgOceiuKqjklhroENU4W";
        $api_key                    = "ieDSTFH8QHHPafg7H0whQB9GaY";
        $api_secret                 = "mgm8wVS9YP93IJmTQtsmR8ZJADDNdlTca5kCizMkC7O7gFDS1j";
        $twitter_timeline           = "user_timeline";  //[mentions_timeline/user_timeline/home_timeline/retweets_of_me]
        //create request
        $request= array(
            'screen_name'       => 'runs_ES',
            'count'             => '3',
            'exclude_replies'   => 'true'
            );
        if (!is_null($last_id)) { //Add to the request if it exits a last_id
            $request['since_id']= $max_id;
        }
        $oauth = array(
            'oauth_consumer_key'        => $api_key,
            'oauth_nonce'               => time(),
            'oauth_signature_method'    => 'HMAC-SHA1',
            'oauth_token'               => $oauth_access_token,
            'oauth_timestamp'           => time(),
            'oauth_version'             => '1.0'
            );
        //merge request and oauth to one array
        $oauth= array_merge($oauth, $request);
        //do some magic
        $base_info=                 buildBaseString("https://api.twitter.com/1.1/statuses/$twitter_timeline.json", 'GET', $oauth);
        $composite_key=             rawurlencode($api_secret).'&'.rawurlencode($oauth_access_token_secret);
        $oauth_signature=           base64_encode(hash_hmac('sha1', $base_info, $composite_key, true));
        $oauth['oauth_signature']=  $oauth_signature;
        //make request
        $header= array(buildAuthorizationHeader($oauth), 'Expect:');
        $options= array(CURLOPT_HTTPHEADER => $header,
                        CURLOPT_HEADER => false,
                        CURLOPT_URL => "https://api.twitter.com/1.1/statuses/$twitter_timeline.json?". http_build_query($request),
                        CURLOPT_RETURNTRANSFER => true,
                        CURLOPT_SSL_VERIFYPEER => false);
        $feed= curl_init();
        curl_setopt_array($feed, $options);
        $json= curl_exec($feed);
        curl_close($feed);
        return $json;
    }
    
    function parse_tweettext($tweet_text) {
        $text= substr($tweet_text, 0, -23);
        $short_url= substr($tweet_text, -23, 23);
        return array ('text'=>$text, 'short_url'=> $short_url);
    }
    
    function parse_tweetdatetime($tweetdatetime) {
        //Thu Aug 21 21:57:26 +0000 2014 Sun Mon Tue Wed Thu Fri Sat
        $months= array('Jan'=>'01', 'Feb'=>'02', 'Mar'=>'03', 'Apr'=>'04', 'May'=>'05', 'Jun'=>'06', 
                        'Jul'=>'07', 'Aug'=>'08', 'Sep'=>'09', 'Oct'=>'10', 'Nov'=>'11', 'Dec'=>'12');
        $GMT= substr($tweetdatetime, -10, 5);
        $year= substr($tweetdatetime, -4, 4);
        $month_str= substr($tweetdatetime, 4, 3);
        $month= $months[$month_str];
        $day= substr($tweetdatetime, 8, 2); 
        $dayofweek= substr($tweetdatetime, 0, 3);
        $time= substr($tweetdatetime, 11, 8);
        $date= $year.'-'.$month.'-'.$day;
        $datetime= $date.' '.$time;
        return array('datetime'=>$datetime, 'dayofweek'=>$dayofweek, 'GMT'=>$GMT);
        //datetime: "YYYY-MM-DD HH:MM:SS", dayofweek: Mon, Tue..., GMT: +####
    }
    
    //First check in the database the last id tweet:
    $query= "SELECT MAX(tweets.id_tweet) AS id_last FROM tweets;";
    $result= exec_query($query);
    $row= mysql_fetch_object($result);
    if ($result!= 0 && mysql_num_rows($result)) { //if error in query or not results
        $last_id= $row->id_last;
    }
    else {
        $last_id= null;
    }
    
    $json= returnTweets($last_id);
    $tweets= json_decode($json, TRUE);
    
    foreach ($tweets as $tweet) {
        $tweet_id= $tweet['id'];
        if (!empty($tweet_id)) { //if array is not empty
            $tweet_parsetext= parse_tweettext($tweet['text']);
            $tweet_text= utf8_encode($tweet_parsetext['text']);
            $tweet_shorturl= $tweet_parsetext['short_url'];
            $tweet_parsedt= parse_tweetdatetime($tweet['created_at']);
            $tweet_datetime= $tweet_parsedt['datetime'];
            $tweet_dayofweek= $tweet_parsedt['dayofweek'];
            $tweet_GMT= $tweet_parsedt['GMT'];
            //Insert the tweet into the database:
            $fields = array(
                'id_tweet' => $tweet_id,
                'text_tweet' => $tweet_text,
                'datetime_tweet' => $tweet_datetime,
                'dayofweek_tweet' => $tweet_dayofweek,
                'GMT_tweet' => $tweet_GMT,
                'shorturl_tweet' => $tweet_shorturl
                );
            $new_id= mysql_insert('tweets', $fields);
        }
    } //end of foreach
    ?>
    

    The function to save the tweets:

    function mysql_insert($table, $inserts) {
        $keys = array_keys($inserts);
        exec_query("START TRANSACTION;");
        $query= 'INSERT INTO `'.$table.'` (`'.implode('`,`', $keys).'`) VALUES (\''.implode('\',\'', $inserts).'\')';
        exec_query($query);
        $id= mysql_insert_id();
        if (mysql_error()) {
            exec_query("ROLLBACK;");
            die("Error: $query");
        }
        else {
            exec_query("COMMIT;");
        }
        return $id;
    }
    
    0 讨论(0)
提交回复
热议问题