zanysoft/laravel-serpapi

通过 SerpApi.com 获取 Google、Bing、Baidu、Ebay、Yahoo、Yandex、Home depot、Naver、Apple、Duckduckgo、Youtube 的搜索结果

1.0.1 2023-08-15 12:45 UTC

This package is not auto-updated.

Last update: 2024-09-11 16:37:34 UTC


README

通过 SerpApi.com 获取 Google、Bing、Baidu、Ebay、Yahoo、Yandex、Home depot、Naver、Apple、Duckduckgo、Youtube 的搜索结果

Latest Stable Version MIT License Build Status Quality Score Total Downloads

这里应该是您的描述。

安装

您可以通过 composer 安装此包

composer require zanysoft/laravel-serpapi

配置

您可以使用以下命令发布配置文件

php artisan vendor:publish --provider="ZanySoft\LaravelSerpApi\SerpApiServiceProvider" --tag="serpapi-config"

这是已发布的配置文件内容

return [
    'api_key' => env('SERPAPI_API_KEY'),
    'search_engine' => env('SERPAPI_ENGINE', 'google')
];

https://serpapi.com/dashboard 获取 "您的 API 密钥"

使用方法

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

use ZanySoft\LaravelSerpApi\Facades\SerpApi;

$client = SerpApi::GoogleSearch();
$query = ["q" => "coffee","location"=>"Austin,Texas"];
$response = $client->get_json($query);
print_r($response)

或者,您可以搜索

  • 使用 SerpApi::BingSearch() 搜索 Bing
  • 使用 SerpApi::BaiduSearch() 搜索 Baidu
  • 使用 SerpApi::EbaySearch() 搜索 Ebay
  • 使用 SerpApi::YahooSearch() 搜索 Yahoo
  • 使用 SerpApi::YandexSearch() 搜索 Yandex
  • 使用 SerpApi::WalmartSearch() 搜索 Walmart
  • 使用 SerpApi::YoutubeSearch() 搜索 Youtube
  • 使用 SerpApi::Search($engine) 搜索 HomeDepot
  • 使用 SerpApi::Search($engine) 搜索 Apple App Store
  • 使用 SerpApi::NaverSearch() 搜索 Naver

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

示例

搜索 API 功能

use ZanySoft\LaravelSerpApi\Facades\SerpApi;

$client = SerpApi::GoogleSearch();

$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 
];

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

位置 API

use ZanySoft\LaravelSerpApi\Facades\SerpApi;

$client = SerpApi::GoogleSearch();

$location_list = $client->get_location('Austin', 3);
print_r($location_list);

它打印出与 Austin(德克萨斯州,德克萨斯州,罗切斯特)匹配的前 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

use ZanySoft\LaravelSerpApi\Facades\SerpApi;

$client = SerpApi::GoogleSearch();

$info = $client->get_account();
print_r($info);

它打印出您的账户信息。

搜索 Google 图片

use ZanySoft\LaravelSerpApi\Facades\SerpApi;

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

foreach($data->images_results as $image_result) {
  print_r($image_result->original);
}

此代码打印出所有图片链接

结论

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

  • isch:Google 图片 API。
  • nws:Google 新闻 API。
  • shop:Google 购物 API。
  • 其他任何 Google 服务都应该能正常工作。
  • (无 tbm 参数):常规 Google 搜索。字段 tbs 允许进一步自定义搜索。

完整的文档在这里。

许可证

MIT 许可证(MIT)。有关更多信息,请参阅许可证文件