2013-06-23 65 views
0

我試圖通過連接到twitter API來檢索Twitter數據,並在下面提出一些請求我的代碼,但我沒有收到任何回報......我只是要求不記名令牌併成功接收它。Twitter API 1.1的問題 - 使用PHP的應用程序唯一身份驗證響應

這是PHP代碼:

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

    count=10&screen_name=twitterapi"; 
$headers = array(
    "GET".$url." HTTP/1.1", 
    "Host: api.twitter.com", 
      "User-Agent: My Twitter App v1.0.23", 
    "Authorization: Bearer ".$bearer_token."", 
    "Content-Type: application/x-www-form-urlencoded;charset=UTF-8", 
); 

$ch = curl_init(); // setup a curl 
curl_setopt($ch, CURLOPT_URL,$url); // set url to send to 
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); // set custom headers 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // return output 
$retrievedhtml = curl_exec ($ch); // execute the curl 

print_r($retrievedhtml); 
使用的print_r沒事的時候

顯示在所有和使用的var_dump當我找到「布爾(假)」

什麼可能是任何想法這個錯誤?

問候,

回答

0

嘗試輸出任何潛在的捲曲的錯誤與

curl_error($ch); 

的curl_exec命令後。這可能會給你一些線索,說明發生了什麼問題。完全空的響應通常指向cURL操作本身出錯的地方。

0

你的頭是錯誤的...不包括

"GET".$url." HTTP/1.1" 
在你的頭

此外,您可以通過

$info = curl_getinfo($ch); 
echo $info["http_code"]; 

200打印出HTTP返回代碼是成功的,在4XX或5XX範圍內的任何手段出事了。

0

我建立在@kiers在Twitter dev discussion中發現的評論基礎上。希望這可以幫助!

<?php 
// Get Token 
$ch = curl_init(); 
curl_setopt($ch,CURLOPT_URL, 'https://api.twitter.com/oauth2/token'); 
curl_setopt($ch,CURLOPT_POST, true); 
$data = array(); 
$data['grant_type'] = "client_credentials"; 
curl_setopt($ch,CURLOPT_POSTFIELDS, $data); 

$screen_name = 'ScreenName'; // add screen name here 
$count = 'HowManyTweets'; // add number of tweets here 
$consumerKey = 'EnterYourTwitterAppKey'; //add your app key 
$consumerSecret = 'EnterYourTwitterAppSecret'; //add your app secret 

curl_setopt($ch,CURLOPT_USERPWD, $consumerKey . ':' . $consumerSecret); 
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true); 
$result = curl_exec($ch); 
curl_close($ch); 
$bearer_token = json_decode($result); 
$bearer = $bearer_token->{'access_token'}; // this is your app token 

// Get Tweets 
$ch = curl_init(); 
curl_setopt($ch,CURLOPT_URL, 'https://api.twitter.com/1.1/statuses/user_timeline.json?count='.$count.'&screen_name='.$screen_name); 
curl_setopt($ch,CURLOPT_HTTPHEADER,array('Authorization: Bearer ' . $bearer)); 
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true); 
$result = curl_exec($ch); 
curl_close($ch); 
$cleanresults = json_decode($result); 

// Release the Kraken! 
echo '<ul id="twitter_update_list">'; 
foreach ($cleanresults as $tweet) { 
// Set up some variables 
$tweet_url = 'http://twitter.com/'.$screen_name.'/statuses/'.$tweet->id_str; // tweet url 
$urls = $tweet->entities->urls; // links 
$retweet = $tweet->retweeted_status->user->screen_name; // there is a retweeted user 
$time = new DateTime($tweet->created_at); // lets grab the date 
$date = date_format($time, 'M j, g:ia'); // and format it accordingly 
$url_find = array(); 
$url_links = array(); 
if ($urls) { 
    if (!is_array($urls)) { 
     $urls = array(); 
    } 
    foreach ($urls as $url) { 
     $theurl = $url->url; 
     if ($theurl) { 
      $url_block = '<a href="'.$theurl.'" target="_blank">'.$theurl.'</a>'; 
      $url_find[] = $theurl; // make array of urls 
      $url_links[] = $url_block; // make array of replacement link blocks for urls in text 
     } 
    } 
} 
if ($retweet) { // add a class for retweets 
    $link_class = ' class="retweet"'; 
} else { 
    $link_class = ''; 
} 
echo '<li'.$link_class.'>'; 
$new_text = preg_replace('#@([\\d\\w]+)#', '<a href="http://twitter.com/$1" target="_blank">$0</a>', $tweet->text); // replace all @mentions with actual links 
$newer_text = preg_replace('/#([\\d\\w]+)/', '<a href="https://twitter.com/search?q=%23$1&src=hash" target="_blank">$0</a>', $new_text); // replace all #tags with actual links 
$text = str_replace($url_find, $url_links, $newer_text); // replace all links with actual links 
echo $text; 
echo '<br /><a class="twt-date" href="'.$tweet_url.'" target="_blank">'.$date.'</a>'; // format the date above 
echo '</li>'; 
} 
echo '</ul>'; 

我把一些文件放在github上,名爲「翻轉鳥」。希望這有助於...

0

我創建了PHP庫,支持純應用程序認證和單用戶OAuth。 https://github.com/vojant/Twitter-php

使用

$twitter = new \TwitterPhp\RestApi($consumerKey,$consumerSecret); 
$connection = $twitter->connectAsApplication(); 
$data = $connection->get('/statuses/user_timeline',array('screen_name' => 'TechCrunch')); 
相關問題