gabrielfs7 / google-trends
PHP Google Trends API
Installs: 3 175
Dependents: 0
Suggesters: 0
Security: 0
Stars: 73
Watchers: 7
Forks: 31
Open Issues: 13
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-12-19 23:21:30 UTC
README
A easier way to search on Google Trends and get a standard response in JSON or PHP DTO.
Dependencies
- PHP 7.2+
- PHP ext-json
Advantages on using this API
- Get standard response that can be easily imported to your BI system.
- No need to have a google account.
- No need for web scraping data from Google Trends UI.
- We deal with Google request token handling for you.
- Allows you to create custom reports that better fit to your business.
Current support
- PSR7 Support
- Related topics Search.
- Related queries Search.
- Interest over time Search.
- Interest by region Search.
- Search by categories.
- Search by location.
- Language support.
- Includes top or rising metrics.
- Search type:
- Web
- Image
- News
- Youtube
- Google Shopping
Usage
Using Open API and PSR7
The library provides PSR7 http message support. Check \GSoares\GoogleTrends\Search\Psr7\Search.
Example:
<?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.
And you can view the open api using swagger editor with (File -> Import URL -> Select openapi.yaml URL).
If you prefer your own implementation, please use the steps bellow:
1) Create a 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();
2) Execute the search you wish to
Related Queries
$result = (new GSoares\GoogleTrends\Search\RelatedQueriesSearch()) ->search($searchFilter) ->jsonSerialize();
JSON response example:
{ "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" } ] }
Related Topics
$result = (new GSoares\GoogleTrends\Search\RelatedTopicsSearch()) ->search($searchFilter) ->jsonSerialize();
JSON response example:
{ "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" } ] }
Interest Over Time
$result = (new GSoares\GoogleTrends\Search\InterestOverTimeSearch()) ->search($relatedSearchUrlBuilder) ->jsonSerialize();
JSON response example:
{ "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 } ] }
Interest By Region
$result = (new GSoares\GoogleTrends\Search\InterestByRegionSearch()) ->search($relatedSearchUrlBuilder) ->jsonSerialize();
JSON response example:
{ "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 } ] }
Installation
The Project is available on Packagist and you can install it using Composer:
composer require gabrielfs7/google-trends
Testing
Start PHP local server:
php -S localhost:8000 web/index.php
Access the api endpoints:
- http://localhost:8000/search-interest-by-region?withTopMetrics=1&withRisingMetrics=1&searchTerm=carros&location=BR
- http://localhost:8000/search-interest-over-time?withTopMetrics=1&withRisingMetrics=1&searchTerm=carros&location=BR
- http://localhost:8000/search-related-topics?withTopMetrics=1&withRisingMetrics=1&searchTerm=carros&location=BR
- http://localhost:8000/search-related-queries?withTopMetrics=1&withRisingMetrics=1&searchTerm=carros&location=BR
Example
After install it you can access an working example here.
Google Categories
You can find the categories available on Google here.
Contributing
I am really happy you can help with this project. If you are interest on how to contribute, please check this page.