forked from anthony-mills/google-places
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgooglePlaces.php
254 lines (200 loc) · 6.77 KB
/
googlePlaces.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
<?php
class googlePlaces
{
public $_outputType = 'json'; //either json, xml or array
public $_errors = array();
protected $_apiKey = '';
protected $_apiUrl = 'https://maps.googleapis.com/maps/api/place';
protected $_apiCallType = '';
protected $_includeDetails = false;
protected $_language = 'en';
protected $_location; // Required - This must be provided as a google.maps.LatLng object.
protected $_query; //Required if using textsearch
protected $_radius; // Required
protected $_types; // Optional - separate type with pipe symbol http://code.google.com/apis/maps/documentation/places/supported_types.html
protected $_name; // Optional
protected $_sensor = 'false'; // Required simply True or False, is the provided $_location coming from GPS?
protected $_reference;
protected $_accuracy;
protected $_pageToken;
protected $_curloptSslVerifypeer = true; // option CURLOPT_SSL_VERIFYPEER with true value working not always
public function __construct($apiKey)
{
$this->_apiKey = $apiKey;
}
public function search()
{
$this->_apiCallType = 'search';
return $this->_apiCall();
}
public function textSearch() {
$this->_apiCallType = 'textsearch';
return $this->_apiCall();
}
public function details()
{
$this->_apiCallType = 'details';
return $this->_apiCall();
}
public function checkIn()
{
$this->_apiCallType = 'checkin-in';
return $this->_apiCall();
}
public function add()
{
$this->_apiCallType = 'add';
return $this->_apiCall();
}
public function delete()
{
$this->_apiCallType = 'delete';
return $this->_apiCall();
}
public function repeat($pageToken)
{
$this->_apiCallType = 'repeat';
$this->_pageToken = $pageToken;
return $this->_apiCall();
}
public function setLocation($location)
{
$this->_location = $location;
}
public function setQuery($query) {
$this->_query = preg_replace('/\s/', '+', $query);
}
public function setRadius($radius)
{
$this->_radius = $radius;
}
public function setTypes($types)
{
$this->_types = $types;
}
public function setLanguage($language)
{
$this->_language = $language;
}
public function setName($name)
{
$this->_name = $name;
}
public function setSensor($sensor)
{
$this->_sensor = $sensor;
}
public function setReference($reference)
{
$this->_reference = $reference;
}
public function setAccuracy($accuracy)
{
$this->_accuracy = $accuracy;
}
public function setIncludeDetails($includeDetails)
{
$this->_includeDetails = $includeDetails;
}
public function setCurloptSslVerifypeer($curloptSslVerifypeer)
{
$this->_curloptSslVerifypeer = $curloptSslVerifypeer;
}
protected function _checkErrors()
{
if(empty($this->_apiCallType)) {
$this->_errors[] = 'API Call Type is required but is missing.';
}
if(empty($this->_apiKey)) {
$this->_errors[] = 'API Key is is required but is missing.';
}
if(($this->_outputType!='json') && ($this->outputType!='xml') && ($this->outputType!='json')) {
$this->_errors[] = 'OutputType is required but is missing.';
}
}
protected function _apiCall()
{
$this->_checkErrors();
if($this->_apiCallType=='add' || $this->_apiCallType=='delete') {
$postUrl = $this->_apiUrl . '/' . $this->_apiCallType . '/' . $this->_outputType . '?key=' . $this->_apiKey . '&sensor=' . $this->_sensor;
if($this->_apiCallType=='add') {
$locationArray = explode(',', $this->_location);
$lat = trim($locationArray[0]);
$lng = trim($locationArray[1]);
$postData = array();
$postData['location']['lat'] = floatval($lat);
$postData['location']['lng'] = floatval($lng);
$postData['accuracy'] = $this->_accuracy;
$postData['name'] = $this->_name;
$postData['types'] = explode('|', $this->_types);
$postData['language'] = $this->_language;
}
if($this->_apiCallType=='delete') {
$postData['reference'] = $this->_reference;
}
$result = json_decode($this->_curlCall($postUrl,json_encode($postData)));
$result->errors = $this->_errors;
return $result;
}
switch ($this->_apiCallType) {
case('search'):
$URLparams = 'location=' . $this->_location . '&radius='.$this->_radius . '&types=' . urlencode($this->_types) . '&language=' . $this->_language . '&name=' . $this->_name . '&sensor=' . $this->_sensor;
break;
case ('textsearch'):
$URLparams = 'query=' . $this->_query . '&location=' . $this->_location . '&radius=' . $this->_radius . '&types=' . urlencode($this->_types) . '&language=' . $this->_language . '&sensor=' . $this->_sensor;
break;
case('details'):
$URLparams = 'reference=' . $this->_reference . '&language=' . $this->_language . '&sensor=' . $this->_sensor;
break;
case('check-in'):
$URLparams = 'reference=' . $this->_reference . '&language=' . $this->_language . '&sensor=' . $this->_sensor;
break;
case('repeat'):
$URLparams = 'radius='.$this->_radius . '&sensor=' . $this->_sensor . '&pagetoken=' . $this->_pageToken;
$this->_apiCallType = 'search';
break;
}
$URLToCall = $this->_apiUrl . '/' . $this->_apiCallType . '/' . $this->_outputType . '?key='.$this->_apiKey . '&' . $URLparams;
$result = json_decode($this->_curlCall($URLToCall), true);
$result['errors'] = $this->_errors;
if(isset($result['status'])&&$result['status']=='OK' && $this->_apiCallType=='details') {
foreach($result['result']['address_components'] as $key=>$component) {
if($component['types'][0]=='street_number') {
$address_street_number = $component['short_name'];
}
if($component['types'][0]=='route') {
$address_street_name = $component['short_name'];
}
if($component['types'][0]=='locality') {
$address_city = $component['short_name'];
}
if($component['types'][0]=='administrative_area_level_1') {
$address_state = $component['short_name'];
}
if($component['types'][0]=='postal_code') {
$address_postal_code = $component['short_name'];
}
}
$result['result']['address_fixed']['street_number'] = $address_street_number;
$result['result']['address_fixed']['address_street_name'] = $address_street_name;
$result['result']['address_fixed']['address_city'] = $address_city;
$result['result']['address_fixed']['address_state'] = $address_state;
$result['result']['address_fixed']['address_postal_code'] = $address_postal_code;
}
return $result;
}
protected function _curlCall($url,$topost = array())
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $this->_curloptSslVerifypeer);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
if (!empty($topost)) {
curl_setopt($ch, CURLOPT_POSTFIELDS, $topost);
}
$body = curl_exec($ch);
curl_close($ch);
return $body;
}
}