Warning : This article uses and relies on the PHP Script cUrl Class (aka eHttpClient) which is mandatory for the following script to work! Make sure you include it before or use your own page retrieval on the URL provided.
This is not an official name but I call it so. It's actually that JavaScript that suggests keywords when you type your search. And the following script will allow you to reuse those suggestion Google gives you.
I made it using JS as $type but Blackhat-seo from blackhat-seo.com told me about the JSON and XML types also available. He also mentioned this cUrl class but, without modesty, I have to say mine looks more friendly.
So I decided to make a one-size-fits-em-all function so you can choose the type you like and the ouput is the same. It uses the json_decode function but if unavailable switches type to something that older PHP can handle too. The code is below:
<?
//$type can be one of xml,json,js
function googleSuggests($keyword,$justKeys=0,$lang="en",$type="xml"){
if(!is_string($keyword)) return false;
//Invalid type: use one of xml,json,js
if(!strstr("'xml','json','js'","'".$type."'")) return false;
//Older php. Function unavailable ... SWITCH!
if(!function_exists("json_decode")){ $type="xml"; }
//Load the url basen on parameters
$url=sprintf(
"http://www.google.com/complete/search?hl=$lang&$type=true&qu=%s",
urlencode($keyword)
);
//We load the page
$hc=new eHttpClient();
$html=$hc->get($url);
//We make it all single-line
$html=preg_replace("/\s+/"," ",$html);
//Decode data based on type
if($type=="json"){
//Decode JSON
$results=json_decode($html);
$results=array_combine($results[1],$results[2]);
}elseif($type=="js"){
//It all starts with the following. Not found = invalid
if(!strstr($html, "new Array(2, ")) return false;
$html=substr(strstr($html,"new Array(2, "),strlen("new Array(2, "));
//We match all results
if(!preg_match_all("/\"([^\"]+)\", \"([0-9,]+)\\s[^\"]+\"/i",
$html, $matches)) return false;
//We build result array
$results=array_combine($matches[1],$matches[2]);
}elseif($type=="xml"){
//I used this .*? in case they insert spaces or stuff between
if(!preg_match_all(
"/<suggestion data=\"([^\"]+)\"\/>.*?".
"<num_queries int=\"([0-9]+)\"\/>/i",
$html,$matches)
) return false;
$results=array_combine($matches[1],$matches[2]);
}
//We fix numbers
if($type!="xml"){
//XML outputs numbers directly! no point for this!
//I used regexp in case they change the word results into smth else
foreach($results as $keyphrase => $count){
$count=preg_replace("/\s([^\s]+)$/i","",$count);
$count=preg_replace("/[^0-9]/i","",$count);
$results[$keyphrase]=(int)$count;
}
}
//We return it. If just keys we dump results count!
if($justKeys) return array_keys($results);
return $results;
}
?>
No matter which of the three types (xml,json,js) you use, output exactly the same. Use whichever and the below will apply.
Just assign the result of the function to an array and output it. Remeber, by setting justKeys to 1 you will no longer get result count but just an array of keyphrases.
<?
print_r(googleSuggests("google suggests"));
?>
Will return:
Array
(
[google suggests] => 12200000
[google suggests labs] => 2240000
[google suggests lab] => 2060000
)
<?
//Here I ask just for keyphrases by setting $justKeys to 1(true)
print_r(googleSuggests("google suggests",1));
?>
Will return:
Array
(
[0] => google suggests
[1] => google suggests labs
[2] => google suggests lab
)
In case it changes, as it did few weeks ago, lemme know and I'll rebuild it or maybe I'll just notice myself.
Post Feedback