2017 © Pedro Peláez
 

library google-trends

PHP Google Trends API

image

gabrielfs7/google-trends

PHP Google Trends API

  • Wednesday, January 14, 2015
  • by gabrielfs7
  • Repository
  • 4 Watchers
  • 32 Stars
  • 442 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 18 Forks
  • 5 Open issues
  • 1 Versions
  • 7 % Grown

The README.md

Google Trends PHP API

Total Downloads Latest Stable Version, (*1)

Branch master Build Status Scrutinizer Code Quality Code Coverage, (*2)

A easier way to search on Google Trends and get a standard response in JSON or PHP DTO., (*3)

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., (*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)

Open_API_Specs, (*7)

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

Swagger_Editor_Instructions, (*9)

If you prefer your own implementation, please use the steps bellow:, (*10)

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

$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"
      }
   ]
}

Interest Over Time

$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
      }
   ]
}

Interest By Region

$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
      }
   ]
}

Installation

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)

  • 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., (*18)

Google Categories

You can find the categories available on Google here., (*19)

Contributing

I am really happy you can help with this project. If you are interest on how to contribute, please check this page., (*20)

The Versions

14/01 2015

dev-master

9999999-dev

PHP Google Trends API

  Sources   Download

MIT

The Requires

 

The Development Requires

by Gabriel Felipe Soares