2

这是应用程序从 Twitter 用户那里检索数据的代码。我正在使用 TwitterAPIExchange api 库。调用失败,false而是返回 boolean - var_dump($string)prints 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);

?>
4

2 回答 2

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 作为参数传递。默认情况下,它将采用身份验证用户的屏幕名称。

于 2013-10-24T12:24:22.987 回答
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 />";
    }
于 2015-02-27T18:58:33.310 回答