如何使用curl从PHP中的Twitter获取流数据

时间:2018-10-18 20:21:40

标签: php twitter twitter-oauth php-curl

我试图从Twitter获取流数据,我在Twitter开发人员部分发现curl以获取流数据

$ curl --request GET 
 --url 'https://api.twitter.com/1.1/search/tweets.json?q=nasa&result_type=popular' 
 --header 'authorization: OAuth oauth_consumer_key="consumer-key-for-app", 
 oauth_nonce="generated-nonce", oauth_signature="generated-signature", 
 oauth_signature_method="HMAC-SHA1", oauth_timestamp="generated-timestamp", 
 oauth_token="access-token-for-authed-user", oauth_version="1.0"'

但是我发现了如何使用curl在http://collaboradev.com/2011/04/01/twitter-oauth-php-tutorial/处获取数据,但是我正在

{"errors":[{"code":215,"message":"Bad Authentication data."}]}

我检查了我的凭据,但无法解决,问题仍然存在:我的代码在:::

 $nonce = time();
        $timestamp = time();
        $oauth = array('oauth_callback' => 'https://localhost/twitter/curl.php',
              'oauth_consumer_key' => 'bGLk7nhcMySEulFeRICCMdmtk',
              'oauth_nonce' => $nonce,
              'oauth_signature_method' => 'HMAC-SHA1',
              'oauth_timestamp' => $timestamp,
              'oauth_version' => '1.0');

        function buildAuthorizationHeader($oauth){
        $r = 'Authorization: OAuth '; //header prefix

        $values = array(); //temporary key=value array
        foreach($oauth as $key=>$value)
        $values[] = "$key=\"" . rawurlencode($value) . "\""; //encode key=value string

        $r .= implode(', ', $values); //reassemble
        return $r; //return full authorization header
        }

        $header = array( buildAuthorizationHeader($oauth), 'Expect:');


        $options = array(CURLOPT_HTTPHEADER => $header, //use our authorization and expect header
                           CURLOPT_HEADER => false, //don't retrieve the header back from Twitter
                           CURLOPT_URL => 'https://api.twitter.com/1.1/search/tweets.json?q=nasa&result_type=popular', //the URI we're sending the request to
                           CURLOPT_POST => true, //this is going to be a POST - required
                           CURLOPT_RETURNTRANSFER => true, //return content as a string, don't echo out directly
                           CURLOPT_SSL_VERIFYPEER => false);



    $ch = curl_init(); //get a channel
    curl_setopt_array($ch, $options); //set options
    $response = curl_exec($ch); //make the call
    curl_close($ch); //hang up
    echo $response;

0 个答案:

没有答案