Twitter API 1.1的问题 - 使用PHP的仅应用程序身份验证响应

时间:2013-06-23 15:50:54

标签: php api twitter twitter-oauth

我正试图通过连接到Twitter API来检索来自Twitter的数据,并在下面提出我的代码请求,但我得不到任何回报......我只是请求了持有人令牌并成功收到了它。

这是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时我发现“bool(false)”

对于这可能出错的任何想法?

此致

4 个答案:

答案 0 :(得分:0)

尝试使用

输出任何潜在的cURL错误
curl_error($ch);

在curl_exec命令之后。这可能会让你知道出了什么问题。完全空的响应通常指向cURL操作本身出错。

答案 1 :(得分:0)

您的标题错误...不包括

"GET".$url." HTTP/1.1"

在你的标题中。

此外,您可以通过

打印出HTTP返回码
$info = curl_getinfo($ch);
echo $info["http_code"];

200是成功的,4xx或5xx范围内的任何内容都意味着出了问题。

答案 2 :(得分: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上,命名为“Flip the Bird”。希望这会有所帮助...

答案 3 :(得分:0)

我创建了支持仅应用程序身份验证和单用户OAuth的PHP库。 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'));