[PHP] Get Google Trends Basic

jaran

New member
This is a basic php script with CURL and XML parsing to get latest Google Trends title. It will provide how to build the script. The script is using alot marketers, researchers, website owners, and SEO experts until now. Here we go,

PHP:
<?php
function get_url($url){
$agent = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.8) Gecko/2009032609 Firefox/3.0.8';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
$result = curl_exec($ch);
return $result;
}
$url = get_url('http://www.google.com/trends/hottrends/atom/feed?pn=p1');
$trends = new SimpleXmlElement($url);
$data = $trends->channel->item;
for($j=0; $j < count($data); $j++) {
echo $data[$j]->title."<br>";
}
?>

You can change "p1" on line 13 with other code number. You can check it all at www.google.com/trends/explore. Basically, "p1" is default for US country. Or you can view the result at my website http://allpreview.net (at homepage). Its support until 46 google trends by country.
 

jaran

New member
ogah said:
i use it in the sidebar of my site www.mupeng.ml

Great !
It also can be using to increase google index. I was using for along ago to playing some queries then I've got the google index over 1 milion indexing until now.
 

ogah

New member
get longer key words
PHP:
<?php
error_reporting(0);
header('Content-Type: text/html; charset=utf-8');
function get_url($url){
$agent = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.8) Gecko/2009032609 Firefox/3.0.8';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
$result = curl_exec($ch);
return $result;
}
$url = get_url('http://www.google.com/trends/hottrends/atom/feed?pn=p1');
/*
$trends = new SimpleXmlElement($url);
$data = $trends->channel->item;
for($j=0; $j < count($data); $j++) {
echo $data[$j]->title."<br>";
}
*/
$url = str_replace('ht:', 'ht', $url);
$feed = simpleXML_load_string($url);
$xmlf = $feed->channel;
$xml = json_encode($xmlf);
$arr = json_decode($xml, TRUE);
$item = $arr['item'];
for($i=0; $i < count($item); $i++) {
	foreach($item[$i]['htnews_item'] as $trend) {
		$trends = strip_tags($trend['htnews_item_title']);
		$trends = str_replace(' ...', '', $trends);
		if(strlen($trends) > 5) {
			echo $trends."<br/>\r\n";
		}
	}
}
?>
or
PHP:
<?php
error_reporting(0);
header('Content-Type: text/html; charset=utf-8');
function get_url($url){
$agent = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.8) Gecko/2009032609 Firefox/3.0.8';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
$result = curl_exec($ch);
return $result;
}
$url = get_url('http://www.google.com/trends/hottrends/atom/feed?pn=p1');
/*
$trends = new SimpleXmlElement($url);
$data = $trends->channel->item;
for($j=0; $j < count($data); $j++) {
echo $data[$j]->title."<br>";
}
*/
$url = str_replace('ht:', 'ht', $url);
$feed = simpleXML_load_string($url);
$xmlf = $feed->channel;
$xml = json_encode($xmlf);
$arr = json_decode($xml, TRUE);
$item = $arr['item'];
for($i=0; $i < count($item); $i++) {
	$trends = strip_tags($item[$i]['htnews_item'][0]['htnews_item_title']);
	$trends = str_replace(' ...', '', $trends);
	if(strlen($trends) > 5) {
		echo $trends."<br/>\r\n";
	}
}
?>


but i not use that feed url.
i use this url
Code:
http://www.google.com/trends/hottrends/widget?pn=p1&tn=10&h=413 (get 10 top trends)
http://www.google.com/trends/hottrends/widget?pn=p1&tn=20&h=413 (get 20 top trends)
this url can get up to 30 top trends