-
Notifications
You must be signed in to change notification settings - Fork 0
/
CitygridOffers.php
168 lines (145 loc) · 6.02 KB
/
CitygridOffers.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
<?php
/**
* @access Public
* @version 1.0
* @Author Utkarsh Singh
* @Details This class is responsible to CitygridOffers API Call
**/
Class CitygridOffers {
public $publisherCode;
public $offerListApiUrl;
public $offerDetailApiUrl;
public $offerListingArray;
public $offerDetailArray;
/*@params
*
*@return
*@constructor to initialize all the class data members
*/
public function __construct($viewObj=''){
$this->offerListingArray = array();
$this->offerDetailArray = array();
$this->offerListApiUrl = 'http://api.citygridmedia.com/content/offers/v2/search/where?';
$this->publisherCode = '<use your API key here>';
$this->offerDetailApiUrl = 'http://api.citygridmedia.com/content/offers/v2/detail?';
ini_set('memory_limit', '2G');
}
/*@params
*
*@function to reset the offerListingArray array
*/
public function __resetOfferListingArray(){
$this->offerListingArray = array();
}
/*@params
*
*@function to reset the offerDetailArray array
*/
public function __resetOfferDetailArray(){
$this->offerDetailArray = array();
}
/*@params
*what, where, tag, rpp, start_date, expires_before, has_budget, sort, source, type, popularity, page, format, placement, has_offers, histograms, i, callback, exclude_tag
*@return array
*@Request for citygrid searchOffers
*/
public function searchOffers($what=null,$where=null,$tag=null,$rpp=20,$start_date=null,$expires_before=null,$has_budget=null,$sort='dist',$source=null,$type=null,$popularity=null,$histograms=null,$page=1,$format='json',$callback=null,$placement=null,$exclude_tag=null,$i=null,$tag_op=null){
set_time_limit(0);
try{
$url = $this->offerListApiUrl;
$publisherCode = $this->publisherCode;
$qStr = "";
if($what!=''){ $qStr .= "what=" . urlencode($what); }
if($where!=''){ $qStr .= "&where=" . urlencode($where); }
if($tag!=''){ $qStr .= "&tag=" . urlencode($tag); }
if($rpp!=''){ $qStr .= "&rpp=" . urlencode($rpp); }
if($start_date!=''){ $qStr .= "&start_date=" . urlencode($start_date); }
if($expires_before!=''){ $qStr .= "&expires_before=" . urlencode($expires_before); }
if($has_budget!=''){ $qStr .= "&has_budget=" . urlencode($has_budget); }
if($sort!=''){ $qStr .= "&sort=" . urlencode($sort); }
if($source!=''){ $qStr .= "&source=" . urlencode($source); }
if($type!=''){ $qStr .= "&type=" . urlencode($type); }
if($popularity!=''){ $qStr .= "&popularity=" . urlencode($popularity); }
if($histograms!=''){ $qStr .= "&histograms=" . urlencode($histograms); }
if($page!=''){ $qStr .= "&page=" . urlencode($page); }
if($format!=''){ $qStr .= "&format=" . urlencode($format); }
if($callback!=''){ $qStr .= "&callback=" . urlencode($callback); }
if($placement!=''){ $qStr .= "&placement=" . urlencode($placement); }
if($exclude_tag!=''){ $qStr .= "&exclude_tag=" . urlencode($exclude_tag); }
if($i!=''){ $qStr .= "&i=" . urlencode($i); }
if($tag_op!=''){ $qStr .= "&tag_op=" . urlencode($tag_op); }
$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);
$totalHits = (isset($searchResponse['results']['total_hits'])) ? $searchResponse['results']['total_hits'] : 0;
$tmpResults = (isset($searchResponse['results']['offers'])) ? $searchResponse['results']['offers'] : array();
$results = array_merge($this->offerListingArray,$tmpResults);
$this->offerListingArray = $results;
if(count($this->offerListingArray)>=$totalHits){
return $this->offerListingArray;
}
else{
$page++;
unset($tmpResults);
unset($results);
set_time_limit(0);
return $this->searchOffers($what,$where,$tag,$rpp,$start_date,$expires_before,$has_budget,$sort,$source,$type,$popularity,$histograms,$page,$format,$callback,$placement,$exclude_tag,$i,$tag_op);
}
}catch(Exception $e){
return $e->getMessage();
}
}
/* @params array of offerlist,format,callback,placement,location_id,i
* @return array of offer details.
* @Request for citygrid offer details
*/
public function fetchOfferDetails($offerListingArray,$format='json',$callback=null,$placement=null,$location_id=null,$i=null){
try{
$url_part = $this->offerDetailApiUrl;
$publisherCode = $this->publisherCode;
$url_part .= "&publisher=" . $publisherCode;
foreach($offerListingArray as $offer){
set_time_limit(0);
ob_start();
$url = '';
$qStr = "";
$id = $offer['id'];
$qStr.='&id='.$id;
if($format!=''){ $qStr .= "&format=" . urlencode($format); }
if($callback!=''){ $qStr .= "&callback=" . urlencode($callback); }
if($placement!=''){ $qStr .= "&placement=" . urlencode($placement); }
if($i!=''){ $qStr .= "&i=" . urlencode($i); }
$url .= $url_part.$qStr;
$curl_handle = curl_init();
curl_setopt($curl_handle, CURLOPT_URL, $url);
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($curl_handle);
curl_close($curl_handle);
$response = json_decode($response);
$responseArray[]= $this->objectToArray($response);
$results = array_merge($this->offerDetailArray,$responseArray);
$this->offerDetailArray = $results;
unset($responseArray);
unset($results);
ob_end_flush();
}
// Return events details list array
return $this->offerDetailArray;
}catch(Exception $e){
return $e->getMessage();
}
}
/* @params object
* @return array.
* @convert object into array.
*/
function objectToArray($object){
return @json_decode(@json_encode($object),1);
}
}