首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >访问对象属性?Google API

访问对象属性?Google API
EN

Stack Overflow用户
提问于 2016-02-16 08:56:05
回答 1查看 68关注 0票数 1
代码语言:javascript
复制
<?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);
}

?>

变量$json的输出为

代码语言:javascript
复制
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 )

我想访问一个对象的属性,但这不起作用:

代码语言:javascript
复制
$jsonNew = $json->{'results'};
print_r($jsonNew);

顺便说一句,它不会抛出错误或警告。它什么也不显示。有什么想法吗?

我用谷歌搜索了一下,但我找不到任何关于Google搜索API和如何使用搜索查询结果的进一步信息。

EN

回答 1

Stack Overflow用户

发布于 2016-02-16 09:43:31

您可以通过$json->responseData->results$x访问所需的元素,其中$x是元素编号。

或者您也可以简单地循环遍历整个过程,如下所示

代码语言:javascript
复制
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>";

}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35421832

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档