我无法从json响应中提取趋势名称和搜索查询
$init = 'http://api.twitter.com/1/trends/1.json';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$init);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
$result = curl_exec($ch);
curl_close($ch);
$obj = json_decode($result);
foreach ($obj->trends as $trend) {
print $trend->query;
print $trend->name;
}
答案 0 :(得分:1)
如果你print_r($obj)
,你会看到趋势是一个子阵列
Array
(
[0] => stdClass Object
(
[as_of] => 2010-09-28T01:32:13Z
[trends] => Array
(
[0] => stdClass Object
(
[query] => BlackBerry+PlayBook
[promoted_content] =>
[url] => http://search.twitter.com/search?q=BlackBerry+PlayBook
[name] => BlackBerry PlayBook
[events] =>
)
.......
所以你必须使用它:
...
foreach ($obj[0]->trends as $trend) {
print $trend->query;
print $trend->name;
}
答案 1 :(得分:0)
试试这个
<?php
$init = 'http://api.twitter.com/1/trends/1.json';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$init);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
$result = curl_exec($ch);
curl_close($ch);
$obj = json_decode($result, true);
foreach ($obj[0]['trends'] as $trend) {
print $trend['query'];
echo "<br>";
print $trend['name'];
echo "<hr>";
}
?>