Skip to content

Instantly share code, notes, and snippets.

@gaffling
Created April 3, 2021 09:45
Show Gist options
  • Save gaffling/2d70259b0130d7f72e46c0a76a85cc69 to your computer and use it in GitHub Desktop.
Save gaffling/2d70259b0130d7f72e46c0a76a85cc69 to your computer and use it in GitHub Desktop.
[Google Suggest] Get all the related keywords from Google Suggest #php #function #GoogleSuggest
/* ----------------------------------------------------------------------------------------------- */
/* [Google Suggest] Get all the related keywords from Google Suggest #php #function #GoogleSuggest */
/* ----------------------------------------------------------------------------------------------- */
function googleSuggestKeywords($keyword, $only_one_keyword_allowed=false) {
if (!function_exists('curl_init')) die('ERROR - Please install cURL on your PHP!');
if ($only_one_keyword_allowed==true) { // IF JUST ONE WORD ALLOWED
$keyword = explode(' ', $keyword);
$keyword = $keyword[0];
}
$url = 'http://google.com/complete/search?output=toolbar&q=' . str_replace(' ', '+', $keyword);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$xml = simplexml_load_string(curl_exec($ch));
curl_close($ch);
$result = $xml->xpath('//@data'); // PARSE THE KEYWORDS
$array = array();
while (list($key, $value) = each($result)) $array[] = (string)$value;
return $array;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment