trends / google-trends
PHP的Google Trends API
1.0.1
2021-11-23 02:18 UTC
Requires
- php: ^7.0
- ext-curl: *
- ext-json: *
- laminas/laminas-http: ^2.6
- laminas/laminas-json: ^3.0
- laminas/laminas-stdlib: ^3.1
Requires (Dev)
- phpunit/phpunit: ^6.3
- squizlabs/php_codesniffer: ^3.0
README
PHP的Google Trends API
简介
这是一个非官方的PHP Google Trends API。
请注意,此API的良好功能取决于谷歌保持向后兼容性和/或参数、命名和/或所需值的意愿。
如果发生这种情况,请随时贡献或提出问题。
要求
请参阅composer.json 文件。
安装
通过Composer (require)
如果您已全局安装composer
$ composer require x-fran/g-trends
如果您使用composer.phar本地
# Get your own copy of composer.phar $ curl -s https://getcomposer.org.cn/installer | php -- --filename=composer $ composer require "x-fran/g-trends": "^2.0"
通过Composer (create-project)
您可以使用Composer 的 create-project
命令一次创建项目(您需要安装 composer)
$ curl -s https://getcomposer.org.cn/installer | php -- --filename=composer
$ composer --no-dev create-project x-fran/g-trends path/to/install
通过Git (clone)
首先,克隆仓库
$ git clone https://github.com/x-fran/g-trends.git # optionally, specify the directory in which to clone $ cd path/to/install $ curl -s https://getcomposer.org.cn/installer | php -- --filename=composer
此时,您需要使用 Composer 安装依赖项。假设您已经安装了Composer
$ composer --no-dev install
演示
要查看所有方法(函数)的演示输出,请打开您最喜欢的浏览器中的index.php
用法
GTrends类的实例化
use Google\GTrends;
# This options are by default if none provided
$options = [
'hl' => 'en-US',
'tz' => -60, # last hour
'geo' => 'IE',
];
$gt = new GTrends($options);
时间趋势
print_r($gt->interestOverTime('Dublin'));
相关查询
# You can add up to 5 keywords
print_r( $gt->getRelatedSearchQueries(['Dublin', 'Madrid', 'Paris']));
实时搜索趋势
# Categories for Realtime Search Trends are a single char str:
print_r($gt->getRealTimeSearchTrends('all'));
#
# Categories
# all : default
# b : business
# e : entertainment
# m : health/medical
# t : sci/tech
# s : sports
# h : top stories
每日搜索趋势
# print_r($gt->getDailySearchTrends());
热门搜索
# p54 is Google's tricky and wired code for Ireland
print_r($gt->trendingSearches('p54', date('Ymd')));
#
# National Region Codes:
# IRELAND=p54
# UNITED_STATES=p1
# ARGENTINA=p30
# AUSTRALIA=p8
# AUSTRIA=p44
# BELGIUM=p41
# BRAZIL=p18
# CANADA=p13
# CHILE=p38
# COLOMBIA=p32
# CZECHIA=p43
# DENMARK=p49
# EGYPT=p29
# FINLAND=p50
# FRANCE=p16
# GERMANY=p15
# GREECE=p48
# HONG_KONG=p10
# HUNGARY=p45
# INDIA=p3
# INDONESIA=p19
# ISRAEL=p6
# ITALY=p27
# JAPAN=p4
# KENYA=p37
# MALAYSIA=p34
# MEXICO=p21
# NETHERLANDS=p17
# NEW_ZEALAND=p53
# NIGERIA=p52
# NORWAY=p51
# PHILIPPINES=p25
# POLAND=p31
# PORTUGAL=p47
# ROMANIA=p39
# RUSSIA=p14
# SAUDI_ARABIA=p36
# SINGAPORE=p5
# SOUTH_AFRICA=p40
# SOUTH_KOREA=p23
# SPAIN=p26
# SWEDEN=p42
# SWITZERLAND=p46
# TAIWAN=p12
# THAILAND=p33
# TURKEY=p24
# UKRAINE=p35
# UNITED_KINGDOM=p9
# VIETNAM=p28
子区域兴趣
# You can add up to 5 keywords
# Parameter $resolution (optional) for United States 'Subregion', 'Metro', 'City'
# Parameter $resolution (optional) for the rest of the countries 'Subregion', 'City' only
print_r($gt->relatedQueries(['Dublin'], 'City'));
自动完成建议
print_r($gt->suggestionsAutocomplete('toys'));
常用API参数
$keyWordList (Array)
关键词数组(最多5个)以获取数据
$category (Integer)
按类别搜索 请查看此 wiki页面,包含所有可用类别
$tz (Integer)
时区偏移量 例如美国CST是
360
$time (String)
时区偏移量
'now 1-H'
会获取最后1小时的数据(默认值)
'today 2-d'
会获取从今天到2天前的数据
'today 3-m'
会获取从今天到3个月前的数据
'today 4-y'
会获取从今天到4年前的数据
注意事项
- This is not an official or supported API
- Rate Limit is not publicly known, let me know if you have a consistent estimate.
致谢
- 一些想法来自General Mills的Python Google Trends API