2016-11-06 84 views
0

我試圖從twitter用戶時間軸中檢索推文,指定其screen_name計數與請求URL。使用curl的Twitter API https請求失敗,出現錯誤

我想,我已經提供了所需的值,產生的所有的Twitter https://dev.twitter.com/oauth/overview/authorizing-requests

繼正好提到的必填字段是我的腳本:

$param_string = 'oauth_consumer_key='.$consumer_key. 
     '&oauth_nonce='.$nonce. 
     '&oauth_signature_method=HMAC-SHA1'. 
     '&oauth_timestamp='.$timestamp. 
     '&oauth_token='.$token. 
     '&oauth_version='.$version. 
     '&screen_name='.rawurlencode($screen_name). 
     '&count='.$count; 

$base_string = 'POST&'.rawurlencode($url).'&'.rawurlencode($param_string); 
$signing_key = rawurlencode($consumer_secret)."&".rawurlencode($token_secret); 
$signature = rawurlencode(base64_encode(hash_hmac('sha1', $base_string, $signing_key))); 

$auth = 'OAuth oauth_consumer_key="'.rawurlencode($consumer_key).'",'. 
    'oauth_nonce="'.rawurlencode($nonce).'",'. 
    'oauth_signature="'.rawurlencode($signature).'",'. 
    'oauth_signature_method="HMAC-SHA1",'. 
    'oauth_timestamp="'.rawurlencode($timestamp).'",'. 
    'oauth_token="'.rawurlencode($token).'",'. 
    'oauth_version="1.0"'; 

我的捲曲請求腳本:

$ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, $url); 
    curl_setopt($ch, CURLOPT_HEADER, false); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, True); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); 
    curl_setopt($ch, CURLOPT_CAINFO, __DIR__."/ca-bundle.crt"); 
    curl_setopt($ch,CURLOPT_CAPATH,__DIR__.'/ca-bundle.crt'); 
    curl_setopt($ch, CURLOPT_HTTPHEADER, array("Authorization: $auth")); 
    $output = curl_exec($ch); 

    if(curl_error($ch)) 
    { 
    echo 'HTTP error:' . curl_error($ch); 
    }else{ 
    echo "<pre>"; 
    var_dump($output); 
    } 
    curl_close($ch); 

雖然我執行我的PHP腳本它顯示以下錯誤,而不是鳴叫:

string(64) "{"errors":[{"code":32,"message":"Could not authenticate you."}]}" 

請讓我知道爲什麼它不起作用?

回答

1

我遇到過同樣的錯誤,但解決了通過true into hash_hmac函數。可以請更新你hash_hmac功能如下,然後再試:

$signature = rawurlencode(base64_encode(hash_hmac('sha1', $base_string, $signing_key,true))); 
+0

感謝您的建議,它的工作對我 – Plycoder

+0

不客氣 – 2016-11-06 20:30:34

相關問題