3bkry/google-trends

PHP Google Trends API

dev-master 2023-12-08 21:08 UTC

This package is not auto-updated.

Last update: 2024-11-10 00:23:47 UTC


README

Total Downloads Latest Stable Version

Branch master Build Status Scrutinizer Code Quality Code Coverage

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.

Open_API_Specs

And you can view the open api using swagger editor with (File -> Import URL -> Select openapi.yaml URL).

Swagger_Editor_Instructions

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:

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.