1

I've been reading this helpful post: http://techslides.com/hacking-the-google-trends-api

It shows how you can use cURL in command line/terminal to request data from google trends, for example;

curl --data "ajax=1&cid=actors&geo=US&date=201310" http://www.google.com/trends/topcharts/trendingchart

Gives you a big block of what I think is JSON. Below is an example of what I am doing to use cURL within my PHP to get data like this- however I cannot find anything that would get the data from the above cURL command to work in PHP like below.

<?php 
    //initialize session
    $url = "http://www.google.com/trends/hottrends/atom/feed?pn=p1";
    $ch = curl_init();
    //set options
    curl_setopt($ch, CURLOPT_URL, $url);
    //execute session
    $data = curl_exec($ch);
    echo $data;
    //close session
    curl_close($ch);
    ?>

How do I go about getting the data from above?

2
  • Well, what you get is a RSS feed. Commented Mar 20, 2016 at 0:28
  • With the second example yes- but with the first I'm using cURL to get JSON but in terminal. I was wondering if there was a way of doing that in PHP like I've done in the second example. Commented Mar 20, 2016 at 0:40

2 Answers 2

2

You can do the same with the PHP cURL extension. You just need to set the options through curl_setopt, so you would do something like this

$url = "http://www.google.com/trends/topcharts/trendingchart";
$fields = "ajax=1&cid=actors&geo=US&date=201310";

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
curl_close($ch);

Now you have the response of the website in $data and you can do whatever you want with it.

You can find the PHP cURL documentation on http://php.net/curl

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, I'm using <?php echo $data; ?> to use this as a variable in jQuery now
1

Try this

// Complete url with paramters
$url = "http://www.google.com/trends/topcharts/trendingchart?ajax=1&cid=actors&geo=US&date=201310";

// Init session
$ch = curl_init();

// Set options
curl_setopt($ch, CURLOPT_URL, $url);

// Set option to return the result instead of echoing it
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

// Execute session
$data = curl_exec($ch);

// Close session
curl_close($ch);

// Dump json decoded result
var_dump(json_decode($data, true));

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.