This repository has been archived by the owner on May 25, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
GoogleImageSearch.php
86 lines (77 loc) · 2.09 KB
/
GoogleImageSearch.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<?php
class GoogleImageSearch {
public $baseUrl = 'https://ajax.googleapis.com/ajax/services/search/images';
public $q = '';
public $v = '1.0';
public $as_filetype = 'jpg';
public $as_rights = '';
public $as_sitesearch = '';
public $rsz = 8;
public $hl = 'en';
public $imgc = 'color';
public $imgcolor = '';
public $imgsz = 'large';
public $imgtype = '';
public $restrict = '';
public $safe = 'off';
public $start = 0;
public $key;
private $imagesUrl;
private $params;
private $cq;
private $rawData;
private $searchCount = 64;
private $loops = 8;
public function __construct( $api_key = NULL ) {
if ( isset( $api_key ) ) {
$this->key = $api_key;
}
$this->imagesUrl = array();
}
public function sendQuery() {
try {
for ( $i = 0; $i < $this->loops; $i++ ) {
$this->cq = new CURLQuery( $this->buildQuery() );
$this->cq->CURLOPT_REFERER = $_SERVER['HTTP_REFERER'];
$this->cq->CURLOPT_RETURNTRANSFER = 1;
$this->rawData = $this->cq->doRequest();
$json = json_decode( $this->rawData );
foreach ( $json->responseData->results as $o ) {
$this->imagesUrl[] = $o->url;
}
$this->start += $this->rsz;
}
}
catch ( Exception $e ) {
die( $e->getMessage() );
}
}
public function getImagesUrl() {
return $this->imagesUrl;
}
public function setSearchCount( $n ) {
$this->searchCount = intval( $n );
$this->loops = intval( $this->searchCount / $this->rsz );
}
private function buildQuery() {
$this->params = array(
'q' => $this->q,
'v' => $this->v,
'as_filetype' => $this->as_filetype,
'as_rights' => $this->as_rights,
'as_sitesearch' => $this->as_sitesearch,
'rsz' => $this->rsz,
'hl' => $this->hl,
'imgc' => $this->imgc,
'imgcolor' => $this->imgcolor,
'imgsz' => $this->imgsz,
'imgtype' => $this->imgtype,
'restrict' => $this->restrict,
'safe' => $this->safe,
'start' => $this->start,
'userip' => $this->userip,
'key' => $this->key
);
return $this->baseUrl . '?' . http_build_query( $this->params );
}
}