A PHP library to read google trends data
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.
//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'));
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.
public function __construct($gsession, $lang='en-US', $token=null)
/**
* 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')
/**
* 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')
/**
* 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')
/**
* 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);
admin@coders11.com