Simplest PHP example for retrieving user_timeline with Twitter API version 1.1

前端 未结 14 2318
盖世英雄少女心
盖世英雄少女心 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 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);
    

提交回复
热议问题