PHP code example of gabrielfs7 / google-trends

1. Go to this page and download the library: Download gabrielfs7/google-trends library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

gabrielfs7 / google-trends example snippets



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();

$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();

$result = (new GSoares\GoogleTrends\Search\RelatedTopicsSearch())
    ->search($searchFilter)
    ->jsonSerialize();

$result = (new GSoares\GoogleTrends\Search\InterestOverTimeSearch())
            ->search($relatedSearchUrlBuilder)
            ->jsonSerialize();

$result = (new GSoares\GoogleTrends\Search\InterestByRegionSearch())
            ->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
      }
   ]
}
shell script
php -S localhost:8000 web/index.php