purewater2011/google-trends

PHP Google Trends API

2.0.3 2020-11-08 10:30 UTC

This package is not auto-updated.

Last update: 2024-09-28 20:51:36 UTC


README

Total Downloads Latest Stable Version

Branch master Build Status Scrutinizer Code Quality Code Coverage

一种更简单的方式在Google Trends上搜索并获得标准的JSON或PHP DTO响应。

依赖项

  • PHP 7.2+
  • PHP ext-json

使用此API的优点

  • 获取可以轻松导入到您BI系统的标准响应。
  • 无需拥有谷歌账户。
  • 无需从Google Trends UI抓取数据。
  • 我们为您处理谷歌请求令牌。
  • 允许您创建更符合您业务的定制报告。

当前支持

  • PSR7 支持
  • 相关主题搜索。
  • 相关查询搜索。
  • 时间趋势搜索。
  • 按地区搜索。
  • 按类别搜索。
  • 按位置搜索。
  • 语言支持。
  • 包括顶级或上升指标。
  • 搜索类型
    • 网络
    • 图片
    • 新闻
    • YouTube
    • 谷歌购物

用法

使用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规范。

Open_API_Specs

您可以使用 swagger编辑器通过(文件 -> 导入URL -> 选择openapi.yaml URL)查看开放API。

Swagger_Editor_Instructions

如果您喜欢自己的实现,请按照以下步骤进行

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 purewater2011/google-trends

测试

启动PHP本地服务器

php -S localhost:8000 web/index.php

访问API端点

示例

安装后,您可以在这里访问一个示例。

谷歌分类

您可以在这里找到谷歌上的可用分类。

贡献

我非常高兴您能为此项目提供帮助。如果您对如何贡献感兴趣,请参阅此页面