pod install
变量$ json的输出是
<?PHP
$ip = $_SERVER['REMOTE_ADDR'];
$searchterm = str_replace(' ', '%20', $_POST["searchterm"]);
if(isset($_POST["submit"])){
// The request also includes the userip parameter which provides the end
// user's IP address. Doing so will help distinguish this legitimate
// server-side traffic from traffic which doesn't come from an end-user.
$url = "https://ajax.googleapis.com/ajax/services/search/web?v=1.0&"
. "q=".$searchterm."&userip=".$ip;
// sendRequest
// note how referer is set manually
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, bla.myserver.net);
$body = curl_exec($ch);
curl_close($ch);
// now, process the JSON string
$json = json_decode($body);
// now have some fun with the results...
}
?>
<form method="POST" action="<?PHP $_SERVER["PHP_SELF"]; ?>">
<p><input type="text" name="searchterm"></p>
<p><input type="submit" name="submit"></p>
</form>
<?PHP
if(isset($_POST["submit"])){
print_r($json);
$jsonNew = $json->{'results'};
print_r($jsonNew);
}
?>
我想访问对象的属性,但这不起作用:
stdClass Object ( [responseData] => stdClass Object ( [results] => Array ( [0] => stdClass Object ( [GsearchResultClass] => GwebSearch [unescapedUrl] => http://www.speedtest.net/ [url] => http://www.speedtest.net/ [visibleUrl] => www.speedtest.net [cacheUrl] => http://www.google.com/search?q=cache:M47_v0xF3m8J:www.speedtest.net [title] => Speedtest.net by Ookla - The Global Broadband Speed Test [titleNoFormatting] => Speedtest.net by Ookla - The Global Broadband Speed Test [content] => Test your Internet connection bandwidth to locations around the world with this interactive broadband speed test from Ookla. ) [1] => stdClass Object ( [GsearchResultClass] => GwebSearch [unescapedUrl] => https://www.test.com/ [url] => https://www.test.com/ [visibleUrl] => www.test.com [cacheUrl] => http://www.google.com/search?q=cache:S92tylTr1V8J:www.test.com [title] => Platform to Create Organizational Testing and Certifications [titleNoFormatting] => Platform to Create Organizational Testing and Certifications [content] => Test.com is a software solution for you to easily create, administer and manage training courses and certification tests, in up to 22 languages. ) [2] => stdClass Object ( [GsearchResultClass] => GwebSearch [unescapedUrl] => https://en.wikipedia.org/wiki/Test [url] => https://en.wikipedia.org/wiki/Test [visibleUrl] => en.wikipedia.org [cacheUrl] => http://www.google.com/search?q=cache:R94CAo00wOYJ:en.wikipedia.org [title] => Test - Wikipedia, the free encyclopedia [titleNoFormatting] => Test - Wikipedia, the free encyclopedia [content] => Test, TEST or Tester may refer to: Test (assessment), an assessment intended to measure the respondents' knowledge or other abilities ... ) [3] => stdClass Object ( [GsearchResultClass] => GwebSearch [unescapedUrl] => https://www.speakeasy.net/speedtest/ [url] => https://www.speakeasy.net/speedtest/ [visibleUrl] => www.speakeasy.net [cacheUrl] => http://www.google.com/search?q=cache:sCEGhiP0qxEJ:www.speakeasy.net [title] => Speakeasy Speed Test - Powered by MegaPath [titleNoFormatting] => Speakeasy Speed Test - Powered by MegaPath [content] => Speakeasy, a MegaPath Brand, offers industry leading business voice, data and IT solutions. We ensure the speed, performance and reliability of all our ... ) ) [cursor] => stdClass Object ( [resultCount] => 258,000,000 [pages] => Array ( [0] => stdClass Object ( [start] => 0 [label] => 1 ) [1] => stdClass Object ( [start] => 4 [label] => 2 ) [2] => stdClass Object ( [start] => 8 [label] => 3 ) [3] => stdClass Object ( [start] => 12 [label] => 4 ) [4] => stdClass Object ( [start] => 16 [label] => 5 ) [5] => stdClass Object ( [start] => 20 [label] => 6 ) [6] => stdClass Object ( [start] => 24 [label] => 7 ) [7] => stdClass Object ( [start] => 28 [label] => 8 ) ) [estimatedResultCount] => 258000000 [currentPageIndex] => 0 [moreResultsUrl] => http://www.google.com/search?oe=utf8&ie=utf8&source=uds&start=0&hl=en&q=test [searchResultTime] => 0.32 ) ) [responseDetails] => [responseStatus] => 200 )
顺便说一句,它不会抛出错误或警告。它什么也没显示。有什么想法吗?
我用谷歌搜索了但我无法找到有关Google搜索API的更多信息以及如何使用搜索查询结果。
答案 0 :(得分:1)
您可以访问$ json-&gt; responseData-&gt;结果[$ x]所需的元素。其中$ x是元素编号。
或者你也可以简单地遍历整个事物
for($x=0;$x<count($json->responseData->results);$x++){
echo "</br>URL: ";
echo $json->responseData->results[$x]->url;
echo "</br>VisibleURL: ";
echo $json->responseData->results[$x]->visibleUrl;
echo "</br>Title: ";
echo $json->responseData->results[$x]->title;
echo "</br>Content: ";
echo $json->responseData->results[$x]->content;
echo "</br>";
}