gabrielfs7 / google-trends
PHP Google Trends API
2.0.3
2020-11-08 10:30 UTC
Requires
- php: >=7.2
- ext-json: *
- guzzlehttp/guzzle: ^6.5
- psr/http-message: ^1.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.16
- phpunit/phpunit: ^8.5
- squizlabs/php_codesniffer: ^3.5
This package is auto-updated.
Last update: 2024-09-19 22:37:47 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
示例
安装后,您可以在此处访问一个工作示例。
Google类别
您可以在此处找到Google上的可用类别。
贡献
我非常高兴您能为此项目提供帮助。如果您想了解如何贡献,请查看此页面。

