Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用Json获取Twitter提要_Php_Json_Twitter_Feed_Tweets - Fatal编程技术网

Php 使用Json获取Twitter提要

Php 使用Json获取Twitter提要,php,json,twitter,feed,tweets,Php,Json,Twitter,Feed,Tweets,我正在运行一个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 ($re

我正在运行一个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.
请注意,当您
打印($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 ...

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