Skip to content

Instantly share code, notes, and snippets.

@JamesTheHacker
Created October 28, 2018 20:15
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 JamesTheHacker/72b7b02e04d28fff5d9874912c0a4ee8 to your computer and use it in GitHub Desktop.
Save JamesTheHacker/72b7b02e04d28fff5d9874912c0a4ee8 to your computer and use it in GitHub Desktop.
Scraping Google Suggestions with PHP
<?php
/*
* This script was created by James Jeffery for use in the following video: https://youtu.be/KNMTmiZQPZU
* This script is completely free - as in free beer :)
*/
if (!isset($argv[1]) || empty($argv[1])) {
exit ("You must provide a keyword");
}
$url = "https://www.google.com/complete/search?";
$query = http_build_query([
"client" => "psy-ab",
"q" => $argv[1]
]);
$ch = curl_init($url . $query);
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false,
]);
$response = curl_exec($ch);
curl_close($ch);
if (!$response) {
exit("No results found");
}
$results = json_decode($response, true);
if (!$results || !isset($results[1]) || !count($results[1])) {
exit("No results found");
}
/*
* You can store the keywords in an array if required
*/
$keywords = array_map(function($result) {
return strip_tags($result[0]);
}, $results[1]);
/*
* Or dump them to STDOUT
*/
foreach($keywords as $keyword) {
print($keyword . PHP_EOL);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment