Google Trends Api

A PHP library to read google trends data

Introduction

There is no official api for google trends, but with this php library you can read google trends data programmatically with a minimum effort. There are methods to get graph data, region trends and related query terms.

Usage Example


//Login with your account to avoid rate limits:
$gs=new GSession('user', 'pass');
$gs->authenticate();

//Create the GTrends object with your language ('es' for spanish for example).
$gt=new GTrends($gs, 'es');

//Add terms and a time range to get the graph data
$gt->addTerm('android')->addTerm('ios')->setTime('2007-11-01 2017-11-01');
var_dump($gt->getGraph('csv')); //Get the data in json, array or csv

//Get the cities where the terms are trending
var_dump($gt->getRegions('csv', 'CITY'));

//Get the related queries
var_dump($gt->getRelated('nexus', 'csv'));
        

Google Trends Api Specifications

The library simulates the requests to the service through curl in the same way as if you were accessing with a browser. The library has two classes, one for authentication and another with around 500 lines of code and various methods to filter the data. The code is exhaustively tested on many different servers, php versions, languages and locations.

Class library instantiation:


public function __construct($gsession, $lang='en-US', $token=null)
        

Get graph data from google trends api:


/**
 * Gets the graph of data defining the relevance over time of the searched terms
 * @param string $datatype DATA TYPE to return: GTrends::DATA_JSON, GTrends::DATA_ARRAY, GTrends::DATA_CSV
 * @return mixed Data returned
 */
public function getGraph($datatype='array')
        

Get region data from google trends api:


/**
 * Gets the ranking of regions where the term is trending
 * @param string $datatype DATA TYPE to return: GTrends::DATA_JSON, GTrends::DATA_ARRAY, GTrends::DATA_CSV
 * @param string $region GTrends::REGION_COUNTRY, GTrends::REGION_CITY. When there is a setGeo() defined, the country will become subregion
 * @return type
 */
public function getRegions($datatype='array', $region='COUNTRY')
        

Get related queries from google trends api:


/**
 * Search for related queries
 * @param strimg $term The term to search for related topics
 * @param string $datatype DATA TYPE to return: GTrends::DATA_JSON, GTrends::DATA_ARRAY, GTrends::DATA_CSV
 * @return type
 */
public function getRelated($term, $datatype='array')
        

Parametrization and filtering methods:


/**
 * Clear the search terms
 */
public function clearTerms();

/**
 * Adds a new term to track
 * @param string $keyword Keyword to track
 * @return GTrends
 */
public function addTerm($keyword);

/**
* @param string $type Filters the data to TYPE_IMAGES, TYPE_NEWS, TYPE_GSHOPPING or TYPE_YOUTUBE. When empty looks for everything
* @return GTrends
*/
function setType($type);

/**
* id of the category to filter results
* @param int $category
* @return GTrends
*/
function setCategory($category);

/**
 * Sets the language for the results
 * @param string $lang language code
 * @return GTrends
 */
function setLang($lang);

/**
 * The time range to filter the results, the default is 'today 5-y'.
 * @param string $time google range time format: 'today 5-y', '2011-11-01 2017-04-18'
 * @return GTrends
 */
function setTime($time);

/**
 * Sets the country to filter the results
 * @param string $geo country code
 * @return GTrends
 */
function setGeo($geo);

        

Download google trends api library


Last update: 18/06/2017
We are not sharing this class anymore, you can buy the full source code with license without limits for 28€



We can provide an online demo before buying, if you have any question feel free to contact us at admin@coders11.com, we will answer immediately. If you need this or similar functionality in any other language like java, c#, perl, python, ruby or even as a webservice, contact us for more information.

Contact

admin@coders11.com