Skip to content

Instantly share code, notes, and snippets.

@zachbrowne
Created October 31, 2011 06:53
Show Gist options
  • Save zachbrowne/1327047 to your computer and use it in GitHub Desktop.
Save zachbrowne/1327047 to your computer and use it in GitHub Desktop.
Generate Google Trends keywords with PHP
<?php
/*
This code is used for get the keywords from the
Google HotTrens.U can use the grabtrends() to get
the curerent trends words. it return an array of keywords
u can change the $url to get coountry specific keywords
ex: $url = "<a class="linkclass" href="http://www.google.co.in/trends/hottrends">http://www.google.co.in/trends/hottrends</a>";
gives the INDIAN keywords
*/
//---- this function returns the keywords---
function grabtrends()
{
$url = "<a class="linkclass" href="http://www.google.com/trends/hottrends">http://www.google.com/trends/hottrends</a>";
global $trendurl;
$url=$trendurl;
$arr = curlgrab($url);
$pattern = '|&sa=X">(.*?)</a>|';
$matches = null;
preg_match_all( $pattern, $arr['page'], $matches );
return $matches[1]; // output is an array of key words
}
//---- functio to get data from a website--/
function curlgrab( $url, $ch = null ) {
if( $ch != null ) {
curl_setopt( $ch, CURLOPT_URL, $url );
}
else
{
$ch = curl_init( $url );
curl_setopt( $ch,CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_COOKIEJAR, 'cookies.txt' );
curl_setopt( $ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01;Windows NT 5.0)" );
}
$arr[ 'ch' ] = $ch;
$arr[ 'page' ] = curl_exec( $ch );
return $arr;
}
?>
// include the above code
$out=grabtrends();
echo '<h2>Google Trends Keywords</h2>';
foreach($out as $keyword)
echo "<h3>".$keyword."</h3>";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment