我正在开展一个基于ISBN号搜索图书价格的项目。大部分时间它都有效,但每次我都会尝试获得非物体属性。它发生在第16行(我现在无法让它破解或者我会复制粘贴确切的消息)。
01: <?php
02:
03: function search($query){
04:
05: $Amazon = "http://www.amazon.com/dp/".$query;
06: $url = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=amazon%20".$query."%20buy%20new.%20price";
07:
08: $body = file_get_contents($url);
09: if (is_null(json_decode($body)))
10: {
11: $myArray = "a";
12: }
13: else
14: {
15: $json = json_decode($body);
16: $stuff = $json->responseData->results[0]->content;
17:
18:
19: $myArray = str_split($stuff);
20: }
21:
22: $out = '$';
23:
24: for ($i = 0; $i <= sizeof($myArray) - 1; $i++)
25: {
26: if ($myArray[$i] == '$')
27: {
28: while($myArray[$i] != '.')
29: {
30: $out = $out.$myArray[$i+1];
31: $i++;
32: }
33: $out = $out.$myArray[$i+1].$myArray[$i+2];
34: break;
35: }
36: }
37: if ($out == '$')
38: {
39: $out = "Amazon does not sell this book";
40: $amazon1=$out;
41: //print_r($out);
42: echo "<br>";
43: }
44: else
45: {
46:
47: $amazon1 ="Buy new from "."<a href=$Amazon>Amazon</a>"." for ".$out;
48: echo "<br>";
49:
50: }
这不是整个代码的开头。问题出在哪里。
答案 0 :(得分:1)
你的代码正在寻找一个对象而不是一个,可能在这里
$stuff = $json->responseData->results[0]->content;
你应该通过首先使用
检查它是否是一个对象来处理这个问题 if(is_object ( $json )){
//do stuff
}