使用 JSON 获取 Twitter Feed


Get Twitter Feed using Json

我正在运行一个php网站,并希望从我的公司Twitter提要中提取最新的推文。下面是我拥有的代码,但是它目前不起作用。我做错了什么?我是否需要更改 Twitter 中的任何设置才能使此方法正常工作?

$responseJson = file_get_contents('http://api.twitter.com/1/statuses/user_timeline.json?screen_name=take_me_fishing&include_rts=1&count=1');
if ($responseJson) 
{
  $response = json_decode($responseJson);                   
  $status = $response->text;
}

$response 的值是一个对象数组。由于您只检索了最后一个,因此您需要访问此数组中的第一项。若要访问第一个元素的"text"属性,请按如下所示更改代码:

if ($responseJson) {
  $response = json_decode($responseJson);
  $status   = $response[0]->text;
}
print $status; // The tweet.

请注意,当您print_r($response)时,您将看到以下结构:

Array
(
  [0] => stdClass Object
    (
      [created_at] => Fri Jun 22 15:00:34 +0000 2012
      [id] => 216183907643699200
      [id_str] => 216183907643699200
      [text] => Help us determine the Top 8 State Parks for boating and fishing:
                http://t.co/SQEzWpru #takemefishing
      ... rest of the tweet object ...