Skip to content

Instantly share code, notes, and snippets.

@furious
Created December 2, 2020 13:01
Show Gist options
  • Save furious/97100040408c21bd624aa84c2927a50b to your computer and use it in GitHub Desktop.
Save furious/97100040408c21bd624aa84c2927a50b to your computer and use it in GitHub Desktop.
Google - Simple Image Search API
<?php
function google_image_search($keyword, $page=0){
$c = curl_init();
curl_setopt_array($c, [
CURLOPT_FOLLOWLOCATION => 1,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_SSL_VERIFYPEER => 0,
CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows Phone 8.1; ARM; Trident/7.0; Touch; WebView/2.0; rv:11.0; IEMobile/11.0; NOKIA; Lumia 525) like Gecko'
]);
curl_setopt($c, CURLOPT_URL, "https://www.google.com/search?q=". urlencode($keyword) ."&tbm=isch&start=". ($page * 20));
$results = curl_exec($c); curl_close($c);
preg_match_all('/imgres\?imgurl=([^&]+)&amp;imgrefurl/', $results, $matches);
return $matches[1];
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment