-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCitygridPlaces.php
executable file
·227 lines (200 loc) · 7.74 KB
/
CitygridPlaces.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
<?php
/**
* @access Public
* @version 1.0
* @Author Utkarsh Singh
* @Details This class is responsible for CitygridPlaces API Call
**/
Class CitygridPlaces {
public $publisherCode;
public $apiSearchPlacesUrl;
public $apiSearchPlacesDetailsUrl;
public $searchPlacesArr;
public $searchPlacesDetailsArr;
public $insertedPublicIdsArr;
public $flag;
/*@params
*
*@return
*@constructor to initialize all the class data members
*/
public function __construct(){
ini_set('memory_limit', '4G');
$this->searchPlacesArr = array();
$this->searchPlacesDetailsArr = array();
$this->insertedPublicIdsArr = array();
$this->publisherCode = '<use your API key here>';
$this->apiSearchPlacesUrl = "http://api.citygridmedia.com/content/places/v2/search/where?";
$this->apiSearchPlacesDetailsUrl = "http://api.citygridmedia.com/content/places/v2/detail?";
$this->flag =0;
}
/*@params
*
*@function to reset the searchPlacesArr array
*/
public function __resetSearchPlacesArr(){
$this->searchPlacesArr = array();
}
/*@params
*
*@function to reset the searchPlacesDetailsArr array
*/
public function __resetSearchPlacesDetailsArr(){
$this->searchPlacesDetailsArr = array();
}
/*@params
*
*@function to reset the flag
*/
public function __resetFlag(){
$this->flag = 0;
}
/*@params
*
*@function to get the flag count
*/
public function __getFlagCount(){
return $this->flag;
}
/*@params
*
*@function to reset the insertedPublicIdsArr array
*/
public function __resetInsertedPublicIdsArr(){
$this->insertedPublicIdsArr = array();
}
/*@params
*
*@function to get the insertedPublicIdsArr array
*/
public function getInsertedPublicIdsArr(){
return $this->insertedPublicIdsArr;
}
/*@params
*what, type, where, page, rpp, sort, format, placement, has_offers, histograms, i
*@return array
*@Request for citygrid searchPlaces
*/
public function searchPlaces($what=null, $type=null, $where=null, $page = 1, $rpp = 20, $sort = 'dist', $format='json', $placement=null, $has_offers=null, $histograms=null, $i=null,$CitygridPlacesObj,$apiRecordArray){
try{
$url = $this->apiSearchPlacesUrl;
$publisherCode = $this->publisherCode;
$qStr = "";
$i = null;
if($what!=''){ $qStr .= "what=" . urlencode($what); }
if($type!=''){ $qStr .= "&type=" . urlencode($type); }
if($where!=''){ $qStr .= "&where=" . urlencode($where); }
$qStr .= "&sort=" . urlencode($sort);
$qStr .= "&page=" . urlencode($page);
$qStr .= "&rpp=" . urlencode($rpp);
if($placement!=''){ $qStr .= "&placement=" . urlencode($placement); }
if($has_offers!=''){ $qStr .= "&has_offers=" . urlencode($has_offers); }
if($histograms!=''){ $qStr .= "&histograms=" . urlencode($histograms); }
if($i!=''){ $qStr .= "&i=" . urlencode($i); }
$qStr .= "&format=" . $format;
$qStr .= "&publisher=" . $publisherCode;
$url .= $qStr;
$curl_handle = curl_init();
curl_setopt($curl_handle, CURLOPT_URL, $url);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
$searchResponse = curl_exec($curl_handle);
curl_close($curl_handle);
$searchResponse = json_decode($searchResponse);
$searchResponse = $this->objectToArray($searchResponse);
if(isset($searchResponse['results']['locations'])){
for($i=0; $i<count($searchResponse['results']['locations']); $i++){
set_time_limit(0);
$this->flag++;
if(isset($searchResponse['results']['locations'][$i]['id'])){
$searchResponse['results']['locations'][$i]['api_name'] = CITY_GRID_PLACES;
if(in_array($searchResponse['results']['locations'][$i]['public_id'],$apiRecordArray)){
set_time_limit(0);
$CitygridPlacesObj->update(array("public_id"=>$searchResponse['results']['locations'][$i]['public_id']),$searchResponse['results']['locations'][$i]);
}else{
set_time_limit(0);
$this->searchPlacesArr[]=$searchResponse['results']['locations'][$i];
if(count($this->searchPlacesArr)>=1000){
$CitygridPlacesObj->batchInsert($this->searchPlacesArr);
$this->searchPlacesArr =array();
}
}
}
}
}
$totalHits = (isset($searchResponse['results']['total_hits'])) ? $searchResponse['results']['total_hits'] : 0;
if($this->flag==$totalHits){
//return $this->searchPlacesArr;
$CitygridPlacesObj->batchInsert($this->searchPlacesArr);
echo "success";
}
else{
$page++;
set_time_limit(0);
return $this->searchPlaces($what, $type, $where, $page, $rpp, $sort, $format, $placement, $has_offers, $histograms, $i,$CitygridPlacesObj,$apiRecordArray);
}
}catch(Exception $e){
return 'Exception';
}
}
/*@params
*id, id_type, phone, customer_only, all_results, review_count, placement, format, callback, i
*@return array
*@Request for citygrid search places detail as per id
*/
public function placesDetail($id=array(), $id_type='cg', $phone=null, $customer_only=null, $all_results=null, $review_count=null, $placement=null, $format='json', $callback=null, $i=0,$cityGridPlacesDetails,$apiRecordArray,$apiRecordDetailsArray){
try{
$client_ip = gethostbyname($_SERVER['HTTP_HOST']);
$qStr = "";
$url = $this->apiSearchPlacesDetailsUrl;
if(count($id) && isset($id[count($this->searchPlacesDetailsArr)])){ $qStr .= "id=" . urlencode($id[count($this->searchPlacesDetailsArr)]); }
if($id_type!=''){ $qStr .= "&id_type=" . urlencode($id_type); }
if($phone!=''){ $qStr .= "&phone=" . urlencode($phone); }
if($customer_only!=''){ $qStr .= "&customer_only=" . urlencode($customer_only); }
if($all_results!=''){ $qStr .= "&all_results=" . urlencode($all_results); }
if($review_count!=''){ $qStr .= "&review_count=" . urlencode($review_count); }
if($placement!=''){ $qStr .= "&placement=" . urlencode($placement); }
if($callback!=''){ $qStr .= "&callback=" . urlencode($callback); }
//if($i!=''){ $qStr .= "&i=" . urlencode($i); }
$qStr .= "&offer_count=0";
$qStr .= "&format=" . $format;
$qStr .= "&client_ip=" . $client_ip;
$qStr .= "&publisher=" . $this->publisherCode;
$url .= $qStr;
$curl_handle = curl_init();
curl_setopt($curl_handle, CURLOPT_URL, $url);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
$searchResponse = curl_exec($curl_handle);
curl_close($curl_handle);
$searchResponse = json_decode($searchResponse);
$searchResponse = $this->objectToArray($searchResponse);
$tmpResults = (isset($searchResponse['locations'][0])) ? $searchResponse['locations'][0] : array();
if(sizeof($tmpResults)){
$tmpResults['api_name'] = CITY_GRID_PLACES;
$tmpResults['parent_listing_id'] = $apiRecordArray[$tmpResults['public_id']];
if(in_array($tmpResults['public_id'],$apiRecordDetailsArray)){
$cityGridPlacesDetails->update(array("public_id"=>$tmpResults['public_id']),$tmpResults);
}else{
$this->searchPlacesDetailsArr[]=$tmpResults;
}
}
if($i>=count($apiRecordArray)){
return $this->searchPlacesDetailsArr;
}
else{
$i++;
return $this->placesDetail($id, $id_type, $phone, $customer_only, $all_results, $review_count, $placement, $format, $callback, $i,$cityGridPlacesDetails,$apiRecordArray,$apiRecordDetailsArray);
}
}
catch(Exception $e){
return 'Exception';
}
}
/*@params
*object
*@return array
*@convert object into array.
*/
function objectToArray($object){
return @json_decode(@json_encode($object),1);
}
}