/
imagesearch.php
38 lines (32 loc) · 1.22 KB
/
imagesearch.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<?php
$query = $_GET['word'];
$num_results = intval($_GET['numresults']);
$url = 'https://ajax.googleapis.com/ajax/services/search/images?v=1.0&q='.$query.'&key=ABQIAAAAnQlJeVUV0i_dm2O2z4-94hSmzGmRmg6UEst9m5TdGw5OShYQdhQu3Dl1RlDo_Li7WbakICcuJtbsAQ&imgtype=clipart&rsz=8';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$body = curl_exec($ch);
curl_close($ch);
$json = json_decode($body, true);
$results = $json['responseData']['results'];
$images = array();
for ($i = 0; $i < min(count($results), $num_results); $i++) {
$img = array();
$img['url'] = $results[$i]['url'];
$img['width'] = $results[$i]['width'];
$img['height'] = $results[$i]['height'];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $img['url']);
curl_setopt($ch, CURLOPT_FILETIME, true);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_REFERER, 'http://laneyk.org');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
$header = curl_exec($ch);
$info = curl_getinfo($ch);
if ($info['http_code'] < 400) {
array_push($images, $img);
}
}
echo json_encode($images);
?>