Twitter API 1.1获得最后3种状态

时间:2013-09-06 15:04:13

标签: php twitter twitter-oauth

根据本教程:     

$twitteruser = "twitterusername";
$notweets = 3;
$consumerkey = "12345";
$consumersecret = "123456789";
$accesstoken = "123456789";
$accesstokensecret = "12345";

function getConnectionWithAccessToken($cons_key, $cons_secret, $oauth_token, $oauth_token_secret) {
  $connection = new TwitterOAuth($cons_key, $cons_secret, $oauth_token, $oauth_token_secret);
  return $connection;
}

$connection = getConnectionWithAccessToken($consumerkey, $consumersecret, $accesstoken, $accesstokensecret);

$tweets = $connection->get("https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=".$twitteruser."&count=".$notweets);

echo json_encode($tweets);
?>

如何从json_encode($tweets);'状态'信息中提取?

1 个答案:

答案 0 :(得分:2)

为什么在这里使用json_encode()

来自Twitter的响应是一个JSON字符串,您应该使用json_decode()将其解码为一个对象/数组并打印所需的值。

$tweetArray = json_decode($tweets, TRUE); // or json_decode($tweets); for an object

foreach($tweetArray as $value) {
    // do the printing ...
}