3bkry / google-trends
PHP Google Trends API
dev-master
2023-12-08 21:08 UTC
Requires
- php: >=7.2
- ext-json: *
- guzzlehttp/guzzle: ^7.2
- psr/http-message: ^2.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.16
- phpunit/phpunit: ^8.5
- squizlabs/php_codesniffer: ^3.5
This package is not auto-updated.
Last update: 2024-09-28 23:38:26 UTC
README
一种更简单的方法在 Google Trends 上搜索并获取标准 JSON 或 PHP DTO 响应。
依赖关系
- PHP 7.2+
- PHP ext-json
使用此 API 的优点
- 获取标准响应,可以轻松导入到您的 BI 系统中。
- 无需拥有 Google 账户。
- 无需从 Google Trends UI 爬取数据。
- 我们为您处理 Google 请求令牌。
- 允许您创建更适合您业务的定制报告。
当前支持
- PSR7 支持
- 相关主题搜索。
- 相关查询搜索。
- 按时间搜索兴趣。
- 按地区搜索兴趣。
- 按类别搜索。
- 按位置搜索。
- 语言支持。
- 包含顶级或上升指标。
- 搜索类型
- 网页
- 图片
- 新闻
- YouTube
- Google 购物
用法
使用 Open API 和 PSR7
该库提供 PSR7 http 消息支持。请参阅 \GSoares\GoogleTrends\Search\Psr7\Search。
示例
<?php use GSoares\GoogleTrends\Search\Psr7\Search; use GSoares\GoogleTrends\Search\RelatedQueriesSearch; use GuzzleHttp\Psr7\ServerRequest; $search = new RelatedQueriesSearch(); // $search = new RelatedTopicsSearch(); // $search = new InterestOverTimeSearch(); // $search = new InterestByRegionSearch(); echo (string)(new Search($search))->search(ServerRequest::fromGlobals())->getBody();
您可以在 此处 检查所有 Open API 规范。
您可以使用 swagger editor(文件 -> 导入 URL -> 选择 openapi.yaml URL)查看开放 API。
如果您想使用自己的实现,请按照以下步骤操作
1) 创建一个带有您限制的 SearchFilter
$searchFilter = (new GSoares\GoogleTrends\Search\SearchFilter()) ->withCategory(0) //All categories ->withSearchTerm('google') ->withLocation('US') ->withinInterval( new DateTimeImmutable('now -7 days'), new DateTimeImmutable('now') ) ->withLanguage('en-US') ->considerWebSearch() # ->considerImageSearch() // Consider only image search # ->considerNewsSearch() // Consider only news search # ->considerYoutubeSearch() // Consider only youtube search # ->considerGoogleShoppingSearch() // Consider only Google Shopping search ->withTopMetrics() ->withRisingMetrics();
2) 执行您想要搜索的操作
相关查询
$result = (new GSoares\GoogleTrends\Search\RelatedQueriesSearch()) ->search($searchFilter) ->jsonSerialize();
JSON 响应示例
{ "searchUrl":"http://www.google.com/trends/...", "totalResults":2, "results":[ { "term":"hair salon", "value":100, "hasData": true, "searchUrl":"http://trends.google.com/...", "metricType":"TOP" }, { "term":"short hair", "value":85, "hasData": true, "searchUrl":"http://trends.google.com/...", "metricType":"RISING" } ] }
相关主题
$result = (new GSoares\GoogleTrends\Search\RelatedTopicsSearch()) ->search($searchFilter) ->jsonSerialize();
JSON 响应示例
{ "searchUrl":"http://www.google.com/trends/...", "totalResults":2, "results":[ { "term":"Google Search - Topic", "value":100, "hasData": true, "searchUrl":"http://trends.google.com/...", "metricType":"TOP" }, { "term":"Google - Technology company", "value":85, "hasData": true, "searchUrl":"http://trends.google.com/...", "metricType":"RISING" } ] }
时间兴趣
$result = (new GSoares\GoogleTrends\Search\InterestOverTimeSearch()) ->search($relatedSearchUrlBuilder) ->jsonSerialize();
JSON 响应示例
{ "searchUrl":"http://www.google.com/trends/...", "totalResults":2, "results":[ { "interestAt": "2020-03-21T00:00:00+00:00", "values": [ 58 ], "firstValue": 58, "hasData": true }, { "interestAt": "2020-03-22T00:00:00+00:00", "values": [ 57 ], "firstValue": 57, "hasData": true } ] }
地区兴趣
$result = (new GSoares\GoogleTrends\Search\InterestByRegionSearch()) ->search($relatedSearchUrlBuilder) ->jsonSerialize();
JSON 响应示例
{ "searchUrl":"http://www.google.com/trends/...", "totalResults":2, "results":[ { "geoCode": "US-RI", "geoName": "Rhode Island", "value": 100, "maxValueIndex": 0, "hasData": true }, { "geoCode": "US-NY", "geoName": "New York", "value": 80, "maxValueIndex": 0, "hasData": true } ] }
安装
该项目可在 Packagist 上找到,您可以使用 Composer 进行安装
composer require gabrielfs7/google-trends
测试
启动 PHP 本地服务器
php -S localhost:8000 web/index.php
访问 API 端点
- https://:8000/search-interest-by-region?withTopMetrics=1&withRisingMetrics=1&searchTerm=carros&location=BR
- https://:8000/search-interest-over-time?withTopMetrics=1&withRisingMetrics=1&searchTerm=carros&location=BR
- https://:8000/search-related-topics?withTopMetrics=1&withRisingMetrics=1&searchTerm=carros&location=BR
- https://:8000/search-related-queries?withTopMetrics=1&withRisingMetrics=1&searchTerm=carros&location=BR
示例
安装后,您可以在此处访问一个工作示例 here。
Google 类别
您可以在 Google 此处 找到可用的类别。
贡献
我非常高兴您可以帮助这个项目。如果您对如何贡献感兴趣,请查看 此页。