Created
December 2, 2020 13:01
-
-
Save furious/97100040408c21bd624aa84c2927a50b to your computer and use it in GitHub Desktop.
Google - Simple Image Search API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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=([^&]+)&imgrefurl/', $results, $matches); | |
return $matches[1]; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment