Simplest PHP example for retrieving user_timeline with Twitter API version 1.1

前端 未结 14 2300
盖世英雄少女心
盖世英雄少女心 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条回答
  •  -上瘾入骨i
    2020-11-21 17:55

    Like stated in other answers, create a Twitter app to get the token, key and secret. Using the code bellow, you can modify request parameters from one spot and avoid typos and similar errors (change $request array in returnTweet() function).

    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 returnTweet(){
        $oauth_access_token         = "x";
        $oauth_access_token_secret  = "x";
        $consumer_key               = "x";
        $consumer_secret            = "x";
    
        $twitter_timeline           = "user_timeline";  //  mentions_timeline / user_timeline / home_timeline / retweets_of_me
    
        //  create request
            $request = array(
                'screen_name'       => 'budidino',
                'count'             => '3'
            );
    
        $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'
        );
    
        //  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($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 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_decode($json, true);
    }
    

    and then just call returnTweet()

提交回复
热议问题