php - Google Custom Search API start=100 导致错误 400

标签 php google-custom-search

我有一个使用 Google Custom Search API 的脚本,遍历多个结果页面。

https://www.googleapis.com/customsearch/v1?key=[[KEY]]&num=10&hl=en&start=0&cx=[[CX]]&q=%22bank%22&sort=date&googlehost=www.google.com

https://www.googleapis.com/customsearch/v1?key=[[KEY]]&num=10&hl=en&start=10&cx=[[CX]]&q=%22bank%22&sort=date&googlehost=www.google.com

https://www.googleapis.com/customsearch/v1?key=[[KEY]]&num=10&hl=en&start=20&cx=[[CX]]&q=%22bank%22&sort=date&googlehost=www.google.com

在上述所有示例中,我都得到了正确的响应。查询响应声称有 17,900 个搜索结果。 但是,当脚本到达 start=100 时:

https://www.googleapis.com/customsearch/v1?key=[[KEY]]&num=10&hl=en&start=100&cx=[[CX]]&q=%22bank%22&sort=date&googlehost=www.google.com

我收到以下响应(这是转换为 PHP 对象的 JSON 响应):

stdClass Object (
        [error] => stdClass Object
            (
                [errors] => Array
                    (
                        [0] => stdClass Object
                            (
                                [domain] => global
                                [reason] => invalid
                                [message] => Invalid Value
                            )
                    )
                [code] => 400
                [message] => Invalid Value
            ) )

尽管事实上我在 start=90 中收到的结果声称下一页存在:

"nextPage": [
   {
    "title": "Google Custom Search - \"bank\"",
    "totalResults": "17900",
    "searchTerms": "\"bank\"",
    "count": 10,
    "startIndex": 100,
    "inputEncoding": "utf8",
    "outputEncoding": "utf8",
    "safe": "off",
    "cx": "[[CX VALUE]]",
    "sort": "date",
    "googleHost": "www.google.com",
    "hl": "en"
   }
  ]

使用 API 显示此无效值错误恰好在 start=92 时出现。 此外,这是每次关键字搜索时出现此错误的确切页面。 对此问题的任何帮助将不胜感激。这是因为这是免费版的 Google 自定义搜索吗?

最佳答案

这个信息一点也不容易找到,我发现它在 google.com 上只有一个地方被正式提及。我在 the Custom Search JSON API docs 中找到了一行描述 nextPage 响应元素:

Note: This API returns up to the first 100 results only.

没有提到这仅限于免费 API。我还发现用户报告确认该限制适用,即使您注册了账单,例如参见 this related SO questionthe linked blog post .

奥托the 100 queries/day limit (与您看到的结果限制相反)有详细记录,并且有关于如何绕过该限制的信息(当然是通过注册计费)。

关于php - Google Custom Search API start=100 导致错误 400,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47632578/

相关文章:

php - Openshift MySQL 5.5 上的 Joomla 3.5.1 内存不足

php - 多个文本字段上的 onBlur 事件

用于区分用户的 PHP 全局变量(登录)

Java SSLException : hostname in certificate didn't match for www. googleapis.com

google-custom-search - 如何通过 Google 自定义搜索搜索整个网络?

php - 使用白名单对用户输入进行清理

javascript - jquery中的ajax请求如何处理504响应头?

search-engine - Google Search API 与专用搜索引擎

google-search - 如何在 Google 搜索 iframe 上设置自定义宽度?

Google Site Search 的 C# 包装器或示例