为什么我在将每个对象作为数组获取时出错?

时间:2012-04-06 06:17:35

标签: php json curl yql

我正在尝试单独打印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 个答案:

答案 0 :(得分:1)

好吧,它会返回由javascript回调函数cbfunc(JSON_RESPONSE_DATA);包装的数据....您可以从开头删除cbfunc(,从结尾删除);并执行{ {1}} ...瞧!...你得到阵列......

希望这有帮助。

答案 1 :(得分:1)

这可能会解决您的问题:

foreach( $info['query']['results']['toplevel']['CompleteSuggestion'] as $i ) {
 echo $i['suggestion']['data'];
 echo "<br>";
}

答案 2 :(得分: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;
}

答案 3 :(得分:0)

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

  

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