-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCitygridReviews.php
executable file
·132 lines (116 loc) · 4.66 KB
/
CitygridReviews.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
<?php
/**
* @access Public
* @version 1.0
* @Author Utkarsh Singh
* @Details This class is responsible for CitygridReviews API Call
**/
Class CitygridReviews {
public $publisherCode;
public $apiSearchReviewsUrl;
public $searchReviewsArr;
public $flag;
/*@params
*
*@return
*@constructor to initialize all the class data members
*/
public function __construct(){
$this->searchReviewsArr = array();
$this->flag = 0;
$this->publisherCode = '<use your API key here>';
$this->apiSearchReviewsUrl = "http://api.citygridmedia.com/content/reviews/v2/search/where?";
}
/*@params
*
*@function to reset the flag
*/
public function __resetFlag(){
$this->flag = 0;
}
/*@params
*
*@function to reset the searchReviewsArr array
*/
public function __resetSearchReviewsArr(){
$this->searchReviewsArr = array();
}
/*@params
*$what, $tag, $where, $listing_id, $customer_only, $rating, $days, $page, $rpp, $format, $placement, $callback, $i, $review_type, $sort
*@return array
*@Request for citygrid searchReviews
*/
public function searchReviews($what=null, $tag=null, $where=null, $listing_id=null, $customer_only=null, $rating=null, $days=null, $page=1, $rpp=20, $format='json', $placement=null, $callback=null, $i=null, $review_type=null, $sort=null, $CitygridReviewsModelObj, $existingReviewsRecordsArray){
try{
$url = $this->apiSearchReviewsUrl;
$publisherCode = $this->publisherCode;
$qStr = "";
if($what!=''){ $qStr .= "what=" . urlencode($what); }
if($tag!=''){ $qStr .= "&tag=" . urlencode($tag); }
if($where!=''){ $qStr .= "&where=" . urlencode($where); }
if($listing_id!=''){ $qStr .= "&listing_id=" . urlencode($listing_id); }
if($customer_only!=''){ $qStr .= "&customer_only=" . urlencode($customer_only); }
if($rating!=''){ $qStr .= "&rating=" . urlencode($rating); }
if($days!=''){ $qStr .= "&days=" . urlencode($days); }
if($review_type!=''){ $qStr .= "&review_type=" . urlencode($review_type); }
$qStr .= "&sort=" . urlencode($sort);
$qStr .= "&page=" . urlencode($page);
$qStr .= "&rpp=" . urlencode($rpp);
if($placement!=''){ $qStr .= "&placement=" . urlencode($placement); }
if($callback!=''){ $qStr .= "&callback=" . urlencode($callback); }
if($i!=''){ $qStr .= "&i=" . urlencode($i); }
$qStr .= "&format=" . $format;
$qStr .= "&publisher=" . $publisherCode;
$url .= $qStr;
//echo "URL : ".$url."<br/>";
$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']['reviews'])){
for($i=0; $i<count($searchResponse['results']['reviews']); $i++){
$this->flag++;
if(isset($searchResponse['results']['reviews'][$i]['review_id'])){
$searchResponse['results']['reviews'][$i]['api_name'] = CITY_GRID_REVIEWS;
$searchResponse['results']['reviews'][$i]['listing_id'] = $listing_id;
if(in_array($searchResponse['results']['reviews'][$i]['review_id'],$existingReviewsRecordsArray)){
set_time_limit(0);
$CitygridReviewsModelObj->update(array("review_id"=>$searchResponse['results']['reviews'][$i]['review_id']),$searchResponse['results']['reviews'][$i]);
}else{
set_time_limit(0);
$this->searchReviewsArr[]=$searchResponse['results']['reviews'][$i];
if(count($this->searchReviewsArr)>=1000){
$CitygridReviewsModelObj->batchInsert($this->searchReviewsArr);
$this->searchReviewsArr =array();
}
}
}
}
}
$totalHits = (isset($searchResponse['results']['total_hits'])) ? $searchResponse['results']['total_hits'] : 0;
if($this->flag==$totalHits){
//return $this->searchReviewsArr;
$CitygridReviewsModelObj->batchInsert($this->searchReviewsArr);
echo "success";
}
else{
$page++;
set_time_limit(0);
return $this->searchReviews($what, $tag, $where, $listing_id, $customer_only, $rating, $days, $page, $rpp, $format, $placement, $callback, $i, $review_type, $sort, $CitygridReviewsModelObj, $existingReviewsRecordsArray);
}
}catch(Exception $e){
return 'Exception';
}
}
/*@params
*object
*@return array
*@convert object into array.
*/
function objectToArray($object){
return @json_decode(@json_encode($object),1);
}
}