【问题标题】:Get Twitter Feed using Json使用 Json 获取 Twitter 提要
【发布时间】:2012-06-24 23:12:16
【问题描述】:

我正在运行一个 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;
}

【问题讨论】:

  • “不工作”是什么意思?发生什么了?代码看起来不错。也许,file_get_contents 失败是因为php.ini 中的指令阻止通过网络读取数据?
  • 您无需更改 Twitter 设置中的任何内容。尝试在浏览器中打开 URL。有用。你的 php 代码一定有问题。
  • 感谢大家的快速回复!事实证明,它就像缺少数组括号一样简单。

标签: php json twitter feed tweets


【解决方案1】:

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

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 ...

【讨论】:

    猜你喜欢
    • 2015-11-18
    • 1970-01-01
    • 2012-01-31
    • 2016-11-06
    • 2015-04-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多