我正在尝试单独打印 API 调用的结果。我正在调用 YQL API 调用。
这是我正在使用的 PHP 脚本
<?php
if (!$_REQUEST["q"]) return;
$query = $_REQUEST["q"];
$q = "'http://google.com/complete/search?output=toolbar&q=$query'";
$query = "select * from xml where url=$q";
$url = "http://query.yahooapis.com/v1/public/yql?q=";
$url .= rawurlencode($query);
$url .= "&format=json&env=store://datatables.org/alltableswithkeys";
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;
}
$data = get_data($url);
// decode the data and get the first result set.
$info = json_decode($data);
foreach( $info['suggestion']['data'] as $i ) {
echo $i;
}
?>
我从 API 调用中得到的结果是
cbfunc({
"query": {
"count": 1,
"created": "2012-04-06T06:13:57Z",
"lang": "en-US",
"diagnostics": {
"publiclyCallable": "true",
"url": {
"execution-start-time": "2",
"execution-stop-time": "59",
"execution-time": "57",
"proxy": "DEFAULT",
"content": "http://google.com/complete/search?output=toolbar&q=fb"
},
"user-time": "59",
"service-time": "57",
"build-version": "26247"
},
"results": {
"toplevel": {
"CompleteSuggestion": [
{
"suggestion": {
"data": "fb"
},
"num_queries": {
"int": "1260000000"
}
},
{
"suggestion": {
"data": "fbi"
},
"num_queries": {
"int": "155000000"
}
},
{
"suggestion": {
"data": "fbi most wanted"
},
"num_queries": {
"int": "54800000"
}
},
{
"suggestion": {
"data": "fbi jobs"
},
"num_queries": {
"int": "119000000"
}
},
{
"suggestion": {
"data": "fb covers"
},
"num_queries": {
"int": "82100000"
}
},
{
"suggestion": {
"data": "fb banners"
},
"num_queries": {
"int": "13300000"
}
},
{
"suggestion": {
"data": "fbook"
},
"num_queries": {
"int": "4050000"
}
},
{
"suggestion": {
"data": "fbisd"
},
"num_queries": {
"int": "84300"
}
},
{
"suggestion": {
"data": "fbanners"
},
"num_queries": {
"int": "789000"
}
},
{
"suggestion": {
"data": "fbo"
},
"num_queries": {
"int": "13000000"
}
}
]
}
}
}
});
我正在尝试迭代建议对象,并尝试打印建议对象的数据属性。
我收到以下错误:
Fatal error: Cannot use object of type stdClass as array