jonasva / google-trends

非官方的 Google Trends PHP API

dev-master 2015-04-10 08:07 UTC

This package is auto-updated.

Last update: 2024-09-14 19:03:51 UTC


README

此非官方API提供了一种查询Google Trends特定数据的简单方法。数据可以以解码的JSON、GoogleTrendsTerm对象或格式化的数组(仅适用于制作自己的图表)的形式返回。不支持CSV导出或iFrame结果。

请注意,这是一个非官方的API。它是否继续工作取决于谷歌。请自行承担风险。

安装

要获取此软件包的最新版本,请在您的 composer.json 文件中要求它。

"jonasva/google-trends": "dev-master"

运行 composer update jonasva/google-trends 来安装它。

使用方法

该API使用会话对象,您需要使用有效的谷歌账号进行认证。这是必要的,因为未经认证的用户在请求几次后就会达到趋势配额限制,从而导致1天的禁令。

首先使用您的谷歌账号凭证初始化一个会话对象。您需要在您的谷歌账号中设置一个恢复邮箱(https://support.google.com/accounts/answer/183726?hl=en),以防您被要求验证自己。将恢复邮箱传递给会话对象,如下所示

    $config = [
        'email'         =>  'my.google.account@gmail.com',
        'password'      =>  'mygooglepassword',
        'recovery-email'  =>  'other.email.address@example.com',
    ];

    $session = (new GoogleSession($config))->authenticate();

然后创建一个请求并添加一些参数。以下示例返回了关于“cycling”和“golf”术语的趋势线图表的标签(日期)和数据点。

    $response = (new GoogleTrendsRequest($session)) // create request
                    ->addTerm('cycling') // add a term to compare
                    ->addTerm('golf') // add a term to compare
                    ->setDateRange(new \DateTime('2014-02-01'), new \DateTime()) // date range
                    ->getGraph() // cid (linechart)
                    ->send(); //execute the request

    $data = $response->getFormattedData(); // return formatted data suitable for creating a line chart

示例

获取2014年2月至现在的比利时最受欢迎的谷歌搜索查询。这与谷歌趋势上的数据相当:https://www.google.com/trends/explore#geo=BE&date=2%2F2014%2015m&cmpt=q&tz=

    $response = (new GoogleTrendsRequest($session))
                    ->setDateRange(new \DateTime('2014-02-01'), new \DateTime()) // date range, if not passed, the past year will be used by default
                    ->setLocation('BE') // For location Belgium
                    ->getTopQueries() // cid (top queries)
                    ->send(); //execute the request

    $data = $response->getTermsObjects(); // return an array of GoogleTrendsTerm objects

获取过去一年与“cycling”相关并在“艺术与娱乐”类别(类别ID 0-3)中的上升查询。

    $response = (new GoogleTrendsRequest($session))
                    ->addTerm('cycling') // term cycling
                    ->setCategory('0-3') // category id for arts & entertainment
                    ->getRisingQueries() // cid (rising queries)
                    ->send(); //execute the request

    $data = $response->getTermsObjects(); // return an array of GoogleTrendsTerm objects

备注

不能使用 getTermsObjects() 方法来处理通过 getGraph() 方法获取的响应。

    $response->getTermsObjects();

要获取响应的原始内容,可以使用以下方法

    $response->getResponseContent();

仅解码响应内容,可以使用此方法

    $response->jsonDecode();

每个请求都有0.1到1.5秒的随机延迟。此设置可以在 GoogleSession 对象中更改。

    $session->setMaxSleepInterval(0); // disable the delay
    $session->setMaxSleepInterval(300); // set the max delay to 3 seconds

要检查您是否已认证,可以在 GoogleSession 实例上使用 checkAuth() 方法。

    $response->checkAuth(); // return bool

还有其他一些选项可用,请查看代码以获取更多信息。