PHP code example of x-fran / g-trends

1. Go to this page and download the library: Download x-fran/g-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/ */

    

x-fran / g-trends example snippets


use XFran\GTrends\GTrends;

// All arguments are optional; these are the defaults
$gt = new GTrends(
    hl: 'en-US',    // interface language
    tz: 0,          // timezone offset in minutes, e.g. 360 for US CST
    geo: 'US',      // country code, '' for worldwide
    time: 'all',    // explore range: 'now 1-H', 'now 1-d', 'today 3-m', 'today 5-y', 'all'
    category: 0,    // explore category id, see getCategories()
);

// Route through a proxy to avoid Google's captcha / rate limits
$gt = new GTrends(geo: 'IE', proxy: [
    'proxy_host' => 'your_proxy_host',
    'proxy_port' => 8000,
    'proxy_user' => 'your_proxy_user',
    'proxy_pass' => 'your_proxy_pass',
]);

// Everything trending in $geo over the last 4, 24, 48 or 168 hours
$gt->getTrendingNow(24);

// Restrict to a topic (ids and names in GTrends::TRENDING_TOPICS)
$gt->getTrendingNow(4, 17); // Sports, last 4 hours

// Each entry:
[
    'keyword'             => 'alabama vs kentucky',
    'geo'                 => 'US',
    'startedAt'           => 1789216200,   // unix timestamp
    'searchVolume'        => 500000,
    'volumeGrowthPercent' => 1000,
    'relatedQueries'      => ['alabama football', 'kentucky football', ...],
    'topics'              => [17],
    'topicNames'          => ['Sports'],
    'newsArticleIds'      => [4814762364, ...],
]

// Google's public RSS feed for the same page: fewer fields, but it carries news headlines/urls
// and it is the only trending feed Google exposes on purpose
$gt->getTrendingNowRss();

$gt->getInterestOverTime('Dublin');                 // ['timelineData' => [...], 'averages' => [...]]
$gt->getInterestOverTime(['Dublin', 'Cork']);

$gt->getRelatedQueries(['Dublin', 'Cork']);         // ['Dublin' => ['rankedList' => ...], 'Cork' => ...]
$gt->getRelatedTopics('Dublin');                    // single keyword only (Google's limitation)

$gt->getComparedGeo('Dublin', 'REGION');            // resolution: COUNTRY (worldwide geo only), REGION, CITY

// Or fetch several widgets with a single explore round-trip
$gt->explore(['Dublin', 'Cork'], [GTrends::TIMESERIES, GTrends::RELATED_QUERIES]);
// => ['TIMESERIES' => [...], 'RELATED_QUERIES' => ['Dublin' => [...], 'Cork' => [...]]]

$gt->getSuggestions('Milwaukee');   // [['mid' => '/m/0c1xr', 'title' => 'Milwaukee', 'type' => 'City in Wisconsin'], ...]
$gt->getCategories();               // category tree for the `category` constructor argument
$gt->getGeo();                      // geo tree for the `geo` constructor argument