0

我正在尝试通过连接到 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

4 回答 4

0

尝试输出任何潜在的 cURL 错误

curl_error($ch);

在 curl_exec 命令之后。这可能会给你一个关于出了什么问题的线索。完全空的响应通常表明 cURL 操作本身出了问题。

于 2013-06-23T15:53:47.047 回答
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'));
于 2013-11-09T20:18:10.543 回答
0

你的标题是错误的......不包括

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

在你的标题中。

此外,您可以通过以下方式打印出 HTTP 返回码

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

200 表示成功,4xx 或 5xx 范围内的任何内容都表示出现问题。

于 2013-06-23T16:13:19.227 回答
0

我是根据@kiers在Twitter 开发者讨论中发现的评论构建的。希望这可以帮助!

<?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”。希望这可以帮助...

于 2013-06-26T17:21:36.283 回答