dev-master
9999999-devPHP Google Trends API
MIT
The Requires
- seostats/seostats dev-master
- php >=5.4
The Development Requires
by Gabriel Felipe Soares
Wallogit.com
2017 © Pedro Peláez
PHP Google Trends API
A easier way to search on Google Trends and get a standard response in JSON or PHP DTO., (*3)
The library provides PSR7 http message support. Check \GSoares\GoogleTrends\Search\Psr7\Search., (*4)
Example:, (*5)
<?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();
You can check all Open API specs here., (*6)
, (*7)
And you can view the open api using swagger editor with (File -> Import URL -> Select openapi.yaml URL)., (*8)
, (*9)
If you prefer your own implementation, please use the steps bellow:, (*10)
SearchFilter with your restrictions$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();
$result = (new GSoares\GoogleTrends\Search\RelatedQueriesSearch())
->search($searchFilter)
->jsonSerialize();
JSON response example:, (*11)
{
"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 response example:, (*12)
{
"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 response example:, (*13)
{
"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 response example:, (*14)
{
"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
}
]
}
The Project is available on Packagist and you can install it using Composer:, (*15)
```shell script composer require gabrielfs7/google-trends, (*16)
### Testing Start PHP local server: ```shell script php -S localhost:8000 web/index.php
Access the api endpoints:, (*17)
After install it you can access an working example here., (*18)
You can find the categories available on Google here., (*19)
I am really happy you can help with this project. If you are interest on how to contribute, please check this page., (*20)
PHP Google Trends API
MIT