这是应用程序从Twitter用户检索数据的代码。我正在使用TwitterAPIExchange api库。调用失败,返回布尔值false
- var_dump($string)
打印bool(false)
。
另外我无法弄清楚为什么当我运行代码时我没有得到任何检索到的数据,即输出是空白的。
<?php
require_once('TwitterAPIExchange.php');
$settings = array(
'oauth_access_token' => "",
'oauth_access_token_secret' => "",
'consumer_key' => "",
'consumer_secret' => ""
);
$url = "https://api.twitter.com/1.1/statuses/mentions_timeline.json";
$requestMethod = "GET";
if (isset($_GET['user'])) {
$user = $_GET['user'];
} else {
$user = "lestaaaaaaa";
}
if (isset($_GET['count'])) {
$count = $_GET['count'];
} else {
$count = 20;
}
$getfield = "?screen_name=".$user."&count=".$count;
$twitter = new TwitterAPIExchange($settings);
$string = $twitter->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest();
var_dump($string);
if (is_array($string))
{
foreach($string as $items)
{
echo "Time and Date of Tweet: ".$items['created_at']."<br />";
echo "Tweet: ". $items['text']."<br />";
echo "Tweeted by: ". $items['user']['name']."<br />";
echo "Screen name: ". $items['user']['screen_name']."<br />";
echo "Followers: ". $items['user']['followers_count']."<br />";
echo "Friends: ". $items['user']['friends_count']."<br />";
echo "Listed: ". $items['user']['listed_count']."<br /><hr />";
}
}
print_r($string);
?>
答案 0 :(得分:1)
来自https://dev.twitter.com/docs/api/1.1/get/statuses/mentions_timeline
Returns the 20 most recent mentions (tweets containing a users's @screen_name) for the authenticating user.
因此,在执行此请求时,您无需将screen_name作为参数传递。默认情况下,它将采用身份验证用户的屏幕名称。
答案 1 :(得分:0)
更新代码
/** URL for REST request, see: https://dev.twitter.com/docs/api/1.1/ **/
$url = "https://api.twitter.com/1.1/statuses/user_timeline.json";
$requestMethod = "GET";
if (isset($_GET['user'])) {
$user = $_GET['user'];
} else {
$user = "lestaaaaaaa";
}
if (isset($_GET['count'])) {
$user = $_GET['count'];
} else {
$count = 20;
}
$getfield = "?screen_name=$user&count=$count";
$twitter = new TwitterAPIExchange($settings);
$string = json_decode($twitter ->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest(),$assoc = TRUE);
if($string["errors"][0]["message"] != "") {
echo "<h3>Sorry, there was a problem.</h3><p>Twitter returned the following error message:</p><p><em>".$string[errors][0]["message"]."</em></p>";
exit();
}
foreach($string as $items){
echo "Time and Date of Tweet: ".$items['created_at']."<br />";
echo "Tweet: ". $items['text']."<br />";
echo "Tweeted by: ". $items['user']['name']."<br />";
echo "Screen name: ". $items['user']['screen_name']."<br />";
echo "Followers: ". $items['user']['followers_count']."<br />";
echo "Friends: ". $items['user']['friends_count']."<br />";
echo "Listed: ". $items['user']['listed_count']."<br /><hr />";
}