orzcc / google-search-results-php
通过 SerpApi.com 获取 Google、Bing、Baidu、Ebay、Yahoo、Yandex、Home depot、Naver、Apple、Duckduckgo、Youtube 的搜索结果
Requires
- php: ^5.5 || ^7.0 || ^8.0
- ext-curl: *
- ext-json: *
Requires (Dev)
- php: ^5.5 || ^7.0 || ^8.0
- phpunit/phpunit: ^9.3
This package is not auto-updated.
Last update: 2024-09-18 11:13:29 UTC
README
此 PHP API 用于抓取和解析 Google、Bing 或 Baidu 结果,使用 SerpApi。
以下服务提供
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 响应 Php 类 GoogleSearch
- 格式化请求到 SerpApi 服务器
- 执行 GET http 请求
- 使用 Ruby JSON 标准库解析 JSON 到 Ruby Hash
或者,您可以使用以下方式搜索
- 使用 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