user2090227
user2090227

Reputation: 83

Scraping Keyword Suggestions from Google

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

Upvotes: 3

Views: 9734

Answers (2)

hank
hank

Reputation: 3768

This is valid JSON, use json_decode and you are done!

var_dump(json_decode('["money",["moneygram","money network","money mutual","money trees lyrics","moneyball","moneypak","money","money converter","money order","money2india"]]'));

edit - complete example;

<?php

function getKeywordSuggestionsFromGoogle($keyword) {
    $keywords = array();
    $data = file_get_contents('http://suggestqueries.google.com/complete/search?output=firefox&client=firefox&hl=en-US&q='.urlencode($keyword));
    if (($data = json_decode($data, true)) !== null) {
        $keywords = $data[1];
    }

    return $keywords;
}

var_dump(getKeywordSuggestionsFromGoogle('money'));

Upvotes: 11

Rid Iculous
Rid Iculous

Reputation: 3962

To get the data as an array use this:

    function gsscrape($keyword) {
      return json_decode(utf8_decode(file_get_contents('http://suggestqueries.google.com/complete/search?output=firefox&client=firefox&hl=en-US&q='.urlencode($keyword))),true);
     }

Upvotes: 3

Related Questions