serpapi / google-search-results-php
通过 SerpApi.com 获取 Google、Bing、Baidu、Ebay、Yahoo、Yandex、Home depot、Naver、Apple、Duckduckgo、Youtube 的搜索结果
Requires
- php: ^5.5 || ^7.0
- ext-curl: *
- ext-json: *
Requires (Dev)
- php: ^5.5 || ^7.0
- phpunit/phpunit: ^9.3
This package is auto-updated.
Last update: 2024-09-09 02:36:08 UTC
README
此 PHP API 旨在使用 SerpApi 搜索和解析 Google、Bing 或 Baidu 结果。
以下服务提供:
SerpApi 提供了一个 脚本生成器,以帮助您快速入门。
安装
必须已安装 Php 7+ 和 composer 依赖项管理工具。
包可通过 Packagist 获取。
快速开始
如果您使用 composer,您可以在项目中添加此包(Packagist 链接)。
$ 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($response)
此示例使用您的密钥运行有关 "咖啡" 的搜索。
SerpApi 服务(后端)
- 使用查询 q = "coffee" 在 Google 上进行搜索
- 解析混乱的 HTML 响应
- 返回标准化的 JSON 响应 The Php class GoogleSearch
- 将请求格式化到 SerpApi 服务器
- 执行 GET HTTP 请求
- 使用 Ruby 标准库将 JSON 解析为 Ruby Hash Et voila..
或者,您可以使用以下方式搜索:
- 使用 BingSearch 类搜索 Bing
- 使用 BaiduSearch 类搜索 Baidu
- 使用 EbaySearch 类搜索 Ebay
- 使用 YahooSearch 类搜索 Yahoo
- 使用 YandexSearch 类搜索 Yandex
- 使用 WalmartSearch 类搜索 Walmart
- 使用 YoutubeSearch 类搜索 Youtube
- 使用 HomeDepotSearch 类搜索 HomeDepot
- 使用 AppleAppStoreSearch 类搜索 Apple App Store
- 使用 NaverSearch 类搜索 Naver
查看游乐场以生成您的代码。 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(getenv("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(getenv("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(getenv("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/
要运行代码。
- git clone https://github.com/serpapi/google-search-results-php
- cd google-search-results-php/example_composer/
- make API_KEY= all
更改日志
- 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
- Travis Dent - https://github.com/tcdent/php-restclient
- 测试框架 - PhpUnit - https://phpunit.de/getting-started/phpunit-7.html