屏幕上没有显示,下面这个有效的代码是什么?我知道收到的数据中有一个名为"text"的JSON参数,但不知道如何将其打印出来?
<?php
$url='http://twitter.com/statuses/user_timeline/twostepmedia.json'; //rss link for the twitter timeline
//print_r(get_data($url)); //dumps the content, you can manipulate as you wish to
$obj = json_decode($data);
print $obj->{'text'};
/* gets the data from a URL */
function get_data($url)
{
$ch = curl_init();
$timeout = 5;
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
?>
Run Code Online (Sandbox Code Playgroud)
这应该工作:
$obj = json_decode(get_data($url));
$text = $obj[0]->text;
Run Code Online (Sandbox Code Playgroud)
尝试类似于var_dump($obj)遇到这样的问题时,最好先尝试一下.在这样做之后,你会立刻明白$obj[0]->text你所追求的是什么.
@ benhowdle89评论:
foreach ($obj as $item) {
$text = $item->text;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
29342 次 |
| 最近记录: |