php 如何使用api获取所有谷歌搜索结果

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14055197/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 06:32:03  来源:igfitidea点击:

how to get ALL google search results using api

phpgoogle-search-api

提问by user1279525

I need to get google search results for query. But using something like this

我需要获取谷歌搜索结果进行查询。但是使用这样的东西

$query = 'Nikita Platonenko';
$url = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=".urlencode($query);
$body = file_get_contents($url);
$json = json_decode($body);
var_dump($json)

i get only 4 results, I've already read about google ajax search but couldn't understand it. Please advise how to get all resulsts, or just first 100 results?

我只得到 4 个结果,我已经阅读了有关 google ajax 搜索的内容,但无法理解。请告知如何获得所有结果,或仅获得前 100 个结果?

回答by Monchito

<?php
$query = 'Nikita%20Platonenko';
$url = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=".$query;

$body = file_get_contents($url);
$json = json_decode($body);

for($x=0;$x<count($json->responseData->results);$x++){

echo "<b>Result ".($x+1)."</b>";
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><br>";

}

?>

As the AJAX Api is now depreciated, you can use a third party service like SerpApito get Google results. They have a GitHubrepository, and it should be easy to integrate:

由于 AJAX Api 现在已贬值,您可以使用SerpApi等第三方服务来获取 Google 搜索结果。他们有一个GitHub存储库,应该很容易集成:

$query = [
    "q" => "Coffee",
    "google_domain" => "google.com",
];

$serp = new GoogleSearchResults();
$json_results = $serp.json($query);

回答by Joseph at SwiftOtter

The answer:

答案:

Here is what I use successfully:

这是我成功使用的内容:

http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=[q]&key=[key]&cx=[account]&rsz=large&userip=[userip]&start=[start]

I don't think you have full control over how many results that can be obtained in the query. But you can control the results size (rsz=large), and where it starts.

我认为您无法完全控制在查询中可以获得多少结果。但是您可以控制结果大小 ( rsz=large) 及其开始位置。

Other notes:

其他注意事项:

In addition, it is always good to include the user's ip address there. Because, Google limits the number of queries based unique ip addresses (meaning, how many from an ip address). So if they are all coming from your server, you will be limited in how many queries you can send, but the limits go way down if you send the user's ip address. In addition, caching the results for a couple of days is an added bonus.

此外,在那里包含用户的 IP 地址总是好的。因为,谷歌限制了基于唯一 ip 地址的查询数量(意思是来自一个 ip 地址的数量)。因此,如果它们都来自您的服务器,那么您可以发送的查询数量将受到限制,但是如果您发送用户的 IP 地址,则限制会降低。此外,将结果缓存几天是一个额外的好处。