I’m currently working to scrape keyword suggestion from Google. This is the script I’m working with:
<?php
function text_between($start,$end,$string) {
if ($start != '') {$temp = explode($start,$string,2);} else {$temp = array('',$string);}
$temp = explode($end,$temp[1],2);
return $temp[0];
}
function gsscrape($keyword) {
$keyword=str_replace(" ","+",$keyword);
global $kw;
$data=file_get_contents('http://suggestqueries.google.com/complete/search?output=firefox&client=firefox&hl=en-US&q='.$keyword);
$data=explode('[',$data,3);
$data=explode('],[',$data[2]);
foreach($data as $temp) {
$kw[]= text_between('"','"',$temp);
}
}
#simple to use, just use yourscriptname.php?keywords
if ($_SERVER['QUERY_STRING']!='') {
gsscrape($_SERVER['QUERY_STRING']);
foreach ($kw as $keyword) {
gsscrape($keyword);
}
//sorted and duplicates removed
sort(array_unique($kw));
#all results echoed with break
foreach ($kw as $keywords) {
echo $keywords. "<br />";
}
}
?>
When accessing directly through the URL Google will give me this response for the keyword money:
["money",["moneygram","money network","money mutual","money trees lyrics","moneyball","moneypak","money","money converter","money order","money2india"]]
However, for some reason when I test it on my website, it’s just showing this:
moneygram
moneygram
What needs to be changed so that it displayed each of the keywords like this?
moneygram, money network, money mutual, money trees lyrics, moneyball, moneypak, money, money converter, money order, money2india
This is valid JSON, use
json_decodeand you are done!edit – complete example;