0

我正在尝试单独打印 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 
4

4 回答 4

1

这可能会解决您的问题:

foreach( $info['query']['results']['toplevel']['CompleteSuggestion'] as $i ) {
 echo $i['suggestion']['data'];
 echo "<br>";
}
于 2012-04-06T08:11:26.087 回答
1

好吧,它正在返回由 javascript 回调函数包装的数据...。您可以从头到尾cbfunc(JSON_RESPONSE_DATA);删除,然后执行...瞧!...您得到了数组...cbfunc();print_r(json_decode(REST_OF_THE_RESPONSE, true));

希望这可以帮助。

于 2012-04-06T06:43:13.443 回答
0

要排列输出,您需要将true参数作为第二个。否则它的返回对象。参见:json_decode

// decode the data and get the first result set.
$info = json_decode($data, true);
//var_dump($info);

foreach( $info['suggestion']['data'] as $i ) {
   echo $i;
}
于 2012-04-06T06:20:46.900 回答
0

尝试使用json_decode($data, true);. 请参阅手册:json_encode()

第二个参数为 TRUE 时,返回的对象将被转换为关联数组。

于 2012-04-06T06:21:00.973 回答