2012-11-06 83 views
0

我使用這個代碼peice的嘗試,並獲得所有最新的鳴叫我的網站上打印,但它的返回自己被未經過身份驗證錯誤。我在Dev部分創建了一個應用程序,並獲取我的使用者和OAuth密鑰,並將它們添加到代碼中的正確位置。Twitter的API 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; 
     } 

     $url = "https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=jameskrawczyk"; 
     $oauth_access_token = "SECURITY"; 
     $oauth_access_token_secret = "SECURITY"; 
     $consumer_key = "SECURITY"; 
     $consumer_secret = "SECURITY"; 

     $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; 



     $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, true); 
     foreach ($twitter_data as $elem) 
     { 
      print_r($elem); 
      echo '<br>'; 
     } 

返回錯誤頁面上

Array ([0] => Array ([message] => Could not authenticate you [code] => 32)) 

回答

2

答案似乎是,該行

$url = "https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=jameskrawczyk"; 

不應該有?screen_name=jameskrawczyk末。

1

你必須使用一個庫,從Twitter
獲取數據「themattharris」是一個很好的和著名圖書館從here

+0

下載它根據自己的API 1.1簡介您仍然可以使用它,它只是不是首選。他們甚至舉例說明如何使用它 – jskrwyk

+0

請投票給我的答覆我不再允許由於投票而提問 –