title: Scraping Keyword Suggestions from Google
body: <p>I'm currently working to scrape keyword suggestion from Google.  This is the script I'm working with:</p>
  
  <pre><code>&lt;?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&amp;client=firefox&amp;hl=en-US&amp;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. "&lt;br /&gt;";
  }
  
  }
  ?&gt;
  </code></pre>
  
  <p>When accessing directly through the URL Google will give me this response for the keyword <code>money</code>:</p>
  
  <pre><code>["money",["moneygram","money network","money mutual","money trees lyrics","moneyball","moneypak","money","money converter","money order","money2india"]]
  </code></pre>
  
  <p>However, for some reason when I test it on my website, it's just showing this:</p>
  
  <pre><code>moneygram
  moneygram
  </code></pre>
  
  <p>What needs to be changed so that it displayed each of the keywords like this?</p>
  
  <pre><code>moneygram, money network, money mutual, money trees lyrics, moneyball, moneypak, money, money converter, money order, money2india
  </code></pre>