Git Product home page Git Product logo

google-trends's Introduction

Google Trends Unofficial PHP API

This unofficial API provides an easy way to query Google Trends for certain data. The data can be returned as decoded JSON, GoogleTrendsTerm objects or as a formatted array (only suited for making your own graph). CSV exports or iFrame results are not supported.

Keep in mind that this is an unofficial API. Whether or not it continues to work depends on Google. Use at your own risk.

Installation

To get the latest version of this package require it in your composer.json file.

"jonasva/google-trends": "dev-master"

Run composer update jonasva/google-trends to install it.

Usage

The API works with a session object, which requires you to authenticate with a valid Google account. This is necessary as unauthenticated users will hit the trends quota limit after just a couple of requests, resulting in a 1-day ban.

First init a session object with your google account credentials. You'll need to have a recovery email setup in your Google account (https://support.google.com/accounts/answer/183726?hl=en), in case you are asked to verify yourself. Pass that recovery email to the session object like below:

    $config = [
        'email'         =>  '[email protected]',
        'password'      =>  'mygooglepassword',
        'recovery-email'  =>  '[email protected]',
    ];

    $session = (new GoogleSession($config))->authenticate();

Then create a request and add some parameters. The example below returns an array of a trend line chart's labels (date) and data points for terms 'cycling' and 'golf'.

    $response = (new GoogleTrendsRequest($session)) // create request
                    ->addTerm('cycling') // add a term to compare
                    ->addTerm('golf') // add a term to compare
                    ->setDateRange(new \DateTime('2014-02-01'), new \DateTime()) // date range
                    ->getGraph() // cid (linechart)
                    ->send(); //execute the request

    $data = $response->getFormattedData(); // return formatted data suitable for creating a line chart

Examples

Fetch the top google search queries in Belgium between February 2014 and now. It's equivalent to this data on Google trends: https://www.google.com/trends/explore#geo=BE&date=2%2F2014%2015m&cmpt=q&tz=

    $response = (new GoogleTrendsRequest($session))
                    ->setDateRange(new \DateTime('2014-02-01'), new \DateTime()) // date range, if not passed, the past year will be used by default
                    ->setLocation('BE') // For location Belgium
                    ->getTopQueries() // cid (top queries)
                    ->send(); //execute the request

    $data = $response->getTermsObjects(); // return an array of GoogleTrendsTerm objects

Fetch the rising queries related to 'cycling' and in category 'Arts & Entertainment' (category id 0-3) for the past year.

    $response = (new GoogleTrendsRequest($session))
                    ->addTerm('cycling') // term cycling
                    ->setCategory('0-3') // category id for arts & entertainment
                    ->getRisingQueries() // cid (rising queries)
                    ->send(); //execute the request

    $data = $response->getTermsObjects(); // return an array of GoogleTrendsTerm objects

Remarks

The getTermsObjects() method cannot be used for a response obtained with the getGraph() method.

    $response->getTermsObjects();

To get a response's raw contents, the following method can be used:

    $response->getResponseContent();

To just json decode response content, you can use this method:

    $response->jsonDecode();

Each request has a random delay between 0.1 and 1.5 seconds. This setting can changed in the GoogleSession object.

    $session->setMaxSleepInterval(0); // disable the delay
    $session->setMaxSleepInterval(300); // set the max delay to 3 seconds

To check if you are authenticated, you can use the checkAuth() method on a GoogleSession instance.

    $response->checkAuth(); // return bool

Some other options are available as well, check the code for more information.

google-trends's People

Contributors

jonasva avatar davemg3 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.