Skip to content

Instantly share code, notes, and snippets.

@joshlewis
Last active July 8, 2021 15:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joshlewis/7882722 to your computer and use it in GitHub Desktop.
Save joshlewis/7882722 to your computer and use it in GitHub Desktop.
A fun little toy written in PHP that will show you google suggest results for any given phrase.
<?php
/*
A script to display the top ten Google suggestions for any given string
First version by Josh Lewis https://github.com/joshlewis
Original script at https://gist.github.com/joshlewis/7882722
*/
if ($argc > 1) {
array_shift($argv);
} else {
echo "Usage: php ".preg_replace('/'.preg_quote(dirname(__FILE__).'/', '/').'/', '', __FILE__)." \"your phrase goes here\"\n";
exit;
}
$rawSearch = implode(' ',$argv);
$queryString = urlencode($rawSearch);
$finalUrl = 'http://google.com/complete/search?output=toolbar&q='.$queryString;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $finalUrl);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$sugg = new SimpleXMLElement(curl_exec($ch));
echo "Google's suggestions for \"".$rawSearch."\":\n\n";
foreach ($sugg->CompleteSuggestion as $item) {
//echo preg_replace('/'.$rawSearch.'/', '...', $item->suggestion['data'])."\n"; //Half-hearted attempt to remove the entered text
echo $item->suggestion['data']."\n"; //Just show the raw suggestions unaltered
}
@joshlewis
Copy link
Author

As of today, this is still working. 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment