meinlieberscholli/google-search-results-php

使用 SerpApi.com 在大规模上抓取 Google、Bing、Baidu、Ebay、Youtube、Walmart 和 Yandex 的搜索结果

2.0.3 2021-12-21 13:31 UTC

This package is auto-updated.

Last update: 2024-09-21 19:20:03 UTC


README

PHP build

此 Php API 用于抓取和解析使用 SerpApi(https://serpapi.com)的 Google、Bing 或 Baidu 结果。

完整文档在此处可查。

提供以下服务:

SerpApi 提供了一个 脚本构建器,帮助您快速入门。

安装

必须已安装 Php 7+ 和 composer 依赖管理工具。

此包可在 Packagist 上获取。

快速入门

如果您使用 composer,您可以通过以下链接添加此包(https://packagist.org.cn/packages/serpapi/google-search-results-php)。

$ composer require serpapi/google-search-results-php

然后在您的脚本中加载此依赖项。

<?php
require __DIR__ . '/vendor/autoload.php';
 ?>

如果没有,您必须克隆此仓库并链接类。

require 'path/to/google-search-results';
require 'path/to/restclient';

https://serpapi.com/dashboard 获取“您的密钥”。

然后您可以开始编写类似以下代码的代码:

$client = new GoogleSearch("your secret key");
$query = ["q" => "coffee","location"=>"Austin,Texas"];
$response = $client->get_json($query);
print_r($json_results)

此示例使用您的密钥运行有关“咖啡”的搜索。

SerpApi 服务(后端)

  • 使用查询:q = "coffee" 在 Google 上进行搜索
  • 解析混乱的 HTML 响应
  • 返回标准化的 JSON 响应 Php 类 GoogleSearch
  • 将请求格式化为 SerpApi 服务器
  • 执行 GET http 请求
  • 使用 Ruby 标准库解析 JSON 到 Ruby Hash,即 voila..

或者,您可以使用以下类进行搜索:

  • Bing 使用 BingSearch 类
  • Baidu 使用 BaiduSearch 类
  • Ebay 使用 EbaySearch 类
  • Yahoo 使用 YahooSearch 类
  • Yandex 使用 YandexSearch 类
  • Walmart 使用 WalmartSearch 类
  • Youtube 使用 YoutubeSearch 类

查看游乐场以生成您的代码。 https://serpapi.com/playground

示例

如何设置 SERP API 密钥

可以使用单例模式在全局范围内设置 SerpApi api_key。

$client = new GoogleSearch();
$client->set_serp_api_key("Your Private Key");

或者

$client = new GoogleSearch("Your Private Key");

搜索 API 功能

$query = [
  "q" =>  "query",
  "google_domain" =>  "Google Domain", 
  "location" =>  "Location Requested", 
  "device" =>  "device",
  "hl" =>  "Google UI Language",
  "gl" =>  "Google Country",
  "safe" =>  "Safe Search Flag",
  "num" =>  "Number of Results",
  "start" =>  "Pagination Offset",
  "serp_api_key" =>  "Your SERP API Key",
  "tbm" => "nws|isch|shop"
  "tbs" => "custom to be search criteria"
  "async" => true|false # allow async 
];

$client = new GoogleSearch("private key");

$html_results = $client->get_html($query);
$json_results = $client->get_json($query);

位置 API

$client = new GoogleSearch($this->API_KEY);
$location_list = $client->get_location('Austin', 3);
print_r($location_list);

它打印出与奥斯汀(德克萨斯州,德克萨斯州,罗切斯特)匹配的前 3 个位置

[{:id=>"585069bdee19ad271e9bc072",
  :google_id=>200635,
  :google_parent_id=>21176,
  :name=>"Austin, TX",
  :canonical_name=>"Austin,TX,Texas,United States",
  :country_code=>"US",
  :target_type=>"DMA Region",
  :reach=>5560000,
  :gps=>[-97.7430608, 30.267153],
  :keys=>["austin", "tx", "texas", "united", "states"]},
  ...]

搜索存档 API

让我们运行一个搜索以获取搜索_id。

$client = new GoogleSearch($this->API_KEY);
$result = $client->get_json($this->QUERY);
$search_id = $result->search_metadata->id

现在让我们从存档中检索之前的搜索。

$archived_result = $client->get_search_archive($search_id);
print_r($archived_result);

它打印出存档中的搜索。

账户 API

$client = new GoogleSearch($this->API_KEY);
$info = $client->get_account();
print_r($info);

它打印出您的账户信息。

搜索 Google 图片

$client = new GoogleSearch($this->API_KEY);
$data = $client->get_json([
  'q' => "Coffee", 
  'tbm' => 'isch'
]);

foreach($data->images_results as $image_result) {
  print_r($image_result->original);
  //to download the image:
  // `wget #{image_result[:original]}`
}

此代码打印出所有图片链接,如果您取消注释带有 wget 的行(linux/osx 下载图片的工具),则会下载图片。

根据规范示例

上述代码已在 test.php 和 example.php 文件中进行测试。要本地运行测试。

export API_KEY='your secret key'
make test example

Composer 示例

查看:https://github.com/serpapi/google-search-results-php/example_composer/

运行代码。

变更日志

  • 2.0
    • 代码重构 SearchResult -> Search
    • 添加 walmart 和 youtube 搜索引擎
  • 1.2.0
    • 添加更多搜索引擎
  • 1.0
    • 第一个稳定版本

结论

serpapi 支持所有主流搜索引擎。Google 提供了对所有主要服务的更高级支持:图片、新闻、购物等。要启用某种搜索类型,必须将字段 tbm(待匹配)设置为

  • isch:Google 图片 API。
  • nws:Google 新闻 API。
  • shop:Google 购物 API。
  • 其他任何 Google 服务都应能直接使用。
  • (没有 tbm 参数):常规 Google 搜索。字段 tbs 允许进一步自定义搜索。

完整文档在此处可查。

作者:Victor Benarbia victor@serpapi.com 了解更多信息:https://serpapi.com

感谢 PHP 的 Rest API