· 4 years ago · Apr 05, 2021, 07:52 PM
1<?php
2// In this example we use very convenient library for performing
3// REST api calls. Library is hosted at http://code.google.com/p/gam-http/
4// Of course you are free to use any other REST client library or even
5// write your own.
6if (!defined('__INCLUDES_PATH')) {
7 require_once '../../Examples/PHP/http.php';
8}
9
10/**
11 * Describes two kinds of authentication. Acts as enum.
12 */
13class AuthType {
14 const BASIC = 1;
15 const HMAC = 2;
16}
17
18class PhpExample {
19 const BASE_HOST = 'api.adf.ly';
20 const HMAC_ALGO = 'sha256';
21
22 private $userId = 0;
23 private $publicKey = '';
24 private $secretKey = '';
25 private $connection = null;
26
27 public function __construct($userId, $publicKey, $secretKey) {
28 $this->connection = Http::connect(self::BASE_HOST, 443, Http::HTTPS);
29 $this->secretKey = $secretKey;
30 $this->publicKey = $publicKey;
31 $this->userId = $userId;
32 }
33 public function auth($username, $password) {
34 return json_decode($this->connection->doPost('v1/auth',$this->getParams(array('username' => $username, 'password' => $password), null)), 1);
35 }
36
37 public function getGroups($page=1) {
38 return json_decode($this->connection->doGet('v1/urlGroups',$this->getParams(array('_page' => $page), AuthType::HMAC)),1);
39 }
40 public function createGroup($name) {
41 return $this->connection->doPost('v1/urlGroups',$this->getParams(array('name' => $name), AuthType::HMAC));
42 }
43
44 public function getAccountDetails() {
45 return json_decode($this->connection->doGet('v1/account',$this->getParams(array(), AuthType::HMAC)), 1);
46 }
47
48 public function updateAccountDetails(array $params=[]) {
49 return json_decode($this->connection->doPut('v1/account',$this->getParams($params, AuthType::HMAC)), 1);
50 }
51
52 public function updatePassword($password, $password1, $password2) {
53 return json_decode($this->connection->doPut('v1/password',$this->getParams(array('password' => $password, 'password1' => $password1, 'password2' => $password2), AuthType::HMAC)), 1);
54 }
55
56 public function expand(array $urls, array $hashes=array()) {
57 $params = array();
58
59 $i = 0;
60 foreach ($urls as $url) {
61 $params[sprintf('url[%d]', $i++)] = $url;
62 }
63
64 $i = 0;
65 foreach ($hashes as $hash) {
66 $params[sprintf('hash[%d]', $i++)] = $hash;
67 }
68
69 return json_decode($this->connection->doGet('v1/expand',$this->getParams($params)),1);
70 }
71
72 public function shorten(array $urls, $domain=false, $advertType=false, $groupId=false, $title=false, $customName=false) {
73 $params = array();
74 if ($domain !== false) $params['domain'] = $domain;
75 if ($advertType !== false) $params['advert_type'] = $advertType;
76 if ($groupId !== false) $params['group_id'] = $groupId;
77 if ($title !== false) $params['title'] = $title;
78 if ($customName !== false) $params['custom_name'] = $customName;
79
80 $i = 0;
81 foreach ($urls as $url) {
82 $params[sprintf('url[%d]', $i++)] = $url;
83 }
84
85 return $this->connection->doPost('v1/shorten',$this->getParams($params));
86 }
87
88 public function getUrls($page=1, $q=null) {
89 $params = array('_page' => $page);
90
91 if ($q) {
92 $params['q'] = $q;
93 }
94
95 return json_decode($this->connection->doGet('v1/urls',$this->getParams($params, AuthType::HMAC)),1);
96 }
97
98 public function getReferrers($urlId=null, $month='') {
99 $params = array();
100 if ($urlId) $params['url_id'] = $urlId;
101 if ($month) $params['month'] = $month;
102
103 return json_decode($this->connection->doGet('v1/referrers',$this->getParams($params, AuthType::HMAC)),1);
104 }
105
106 public function getDomains() {
107 $params = array();
108 return json_decode($this->connection->doGet('v1/domains',$this->getParams($params, AuthType::HMAC)),1);
109 }
110
111 public function getAccountCountries() {
112 return json_decode($this->connection->doGet('v1/accountCountries',$this->getParams(array(), AuthType::BASIC)), 1);
113 }
114
115 public function getAccountPubReferrals($fromDate='', $toDate='', $page=1, $includeBanned=0) {
116 $params = array('fromDate' => $fromDate, 'toDate' => $toDate, '_page' => $page, 'includeBanned' => $includeBanned);
117 return json_decode($this->connection->doGet('v1/accountPubReferrals', $this->getParams($params, AuthType::HMAC)), 1);
118 }
119
120 public function getAccountAdvReferrals($fromDate='', $toDate='', $page=1, $includeBanned=0) {
121 $params = array('fromDate' => $fromDate, 'toDate' => $toDate, '_page' => $page, 'includeBanned' => $includeBanned);
122 return json_decode($this->connection->doGet('v1/accountAdvReferrals', $this->getParams($params, AuthType::HMAC)), 1);
123 }
124
125 public function getAccountPopReferrals($fromDate='', $toDate='', $page=1, $includeBanned=0) {
126 $params = array('fromDate' => $fromDate, 'toDate' => $toDate, '_page' => $page, 'includeBanned' => $includeBanned);
127 return json_decode($this->connection->doGet('v1/accountPopReferrals', $this->getParams($params, AuthType::HMAC)), 1);
128 }
129
130 public function getAccountTotalReferrals($includeBanned=0) {
131 $params = array('includeBanned' => $includeBanned);
132 return json_decode($this->connection->doGet('v1/accountTotalReferrals', $this->getParams($params, AuthType::HMAC)), 1);
133 }
134
135 public function getCountries($urlId=null, $month='') {
136 $params = array();
137 if ($urlId) $params['url_id'] = $urlId;
138 if ($month) $params['month'] = $month;
139
140 return json_decode($this->connection->doGet('v1/countries',$this->getParams($params, AuthType::HMAC)),1);
141 }
142 public function getAnnouncements($type=null) {
143 $params = array();
144 if (!empty($type) && in_array($type,array(1,2))) $params['type'] = $type;
145
146 return json_decode($this->connection->doGet('v1/announcements',$this->getParams($params, AuthType::HMAC)),1);
147 }
148
149
150 public function getPublisherReferrals() {
151 return json_decode($this->connection->doGet('v1/publisherReferralStats',$this->getParams(array(), AuthType::HMAC)),1);
152 }
153 public function getAdvertiserReferrals() {
154 return json_decode($this->connection->doGet('v1/advertiserReferralStats',$this->getParams(array(), AuthType::HMAC)),1);
155 }
156
157 public function getWithdraw() {
158 return json_decode($this->connection->doGet('v1/withdraw',$this->getParams(array(), AuthType::HMAC)), 1);
159 }
160 public function getWithdrawalTransactions() {
161 return json_decode($this->connection->doGet('v1/withdrawalTransactions',$this->getParams(array(), AuthType::HMAC)),1);
162 }
163
164 public function withdrawRequestInitiate() {
165 return json_decode($this->connection->doGet('v1/requestWithdraw',$this->getParams(array(), AuthType::HMAC)), 1);
166 }
167
168 public function withdrawRequestCancel() {
169 return json_decode($this->connection->doDelete('v1/requestWithdraw',$this->getParams(array(), AuthType::HMAC)), 1);
170 }
171
172 public function getPublisherStats($date = null, $urlId = 0){
173 $params = array();
174 if(!empty($date)) $params['date'] = $date;
175 if(!empty($urlId)) $params['urlId'] = $urlId;
176
177 return json_decode($this->connection->doGet('v1/publisherStats',$this->getParams($params, AuthType::HMAC)),1);
178 }
179 public function getProfile() {
180 echo $this->connection->doGet('v1/profile',$this->getParams(array(), AuthType::HMAC));
181 die;
182 }
183 public function getAdvertiserCampaigns( $fromDate = null, $toDate = null,$adType = null, $adFilter = null){
184 if(!empty($fromDate)) $params['fromDate'] = $fromDate;
185 if(!empty($toDate)) $params['toDate'] = $toDate;
186 if(!empty($adType)) $params['adType'] = $adType;
187 if(!empty($adFilter)) $params['adFilter'] = $adFilter;
188
189 return json_decode($this->connection->doGet('v1/advertiserCampaigns',$this->getParams(array(), AuthType::HMAC)),1);
190 }
191 public function getAdvertiserGraph($date = null, $websiteId = 0,$adType = null, $adFilter = null){
192 $params = array();
193 if(!empty($date)) $params['date'] = $date;
194 if(!empty($websiteId)) $params['websiteId'] = $websiteId;
195 if(!empty($adType)) $params['adType'] = $adType;
196 if(!empty($adFilter)) $params['adFilter'] = $adFilter;
197
198 return json_decode($this->connection->doGet('v1/advertiserGraph',$this->getParams($params, AuthType::HMAC)),1);
199 }
200 public function getAdvertiserCampaignParts($campaignId, $fromDate = null, $toDate = null, $adType = null, $adFilter = null){
201 $params = array('campaignId' => $campaignId);
202 if(!empty($fromDate)) $params['fromDate'] = $fromDate;
203 if(!empty($toDate)) $params['toDate'] = $toDate;
204 if(!empty($adType)) $params['adType'] = $adType;
205 if(!empty($adFilter)) $params['adFilter'] = $adFilter;
206
207 return json_decode($this->connection->doGet('v1/advertiserCampaignParts',$this->getParams($params, AuthType::HMAC)),1);
208 }
209
210 public function updateUrl($id, $url=false, $advertType=false, $title=false, $groupId=false, $fbDescription=false, $fbImage=false) {
211 $params = array();
212
213 if ($url !== false) $params['url'] = $url;
214 if ($advertType !== false) $params['advert_type'] = $advertType;
215 if ($title !== false) $params['title'] = $title;
216 if ($groupId !== false) $params['group_id'] = $groupId;
217 if ($fbDescription !== false) $params['fb_description'] = $fbDescription;
218 if ($fbImage !== false) $params['fb_image'] = $fbImage;
219
220 return json_decode($this->connection->doPut('v1/urls/' . $id,$this->getParams($params, AuthType::HMAC)),1);
221 }
222
223 public function deleteUrl($id) {
224 return json_decode($this->connection->doDelete('v1/urls/' . $id,$this->getParams(array(), AuthType::HMAC)),1);
225 }
226
227 /**
228 * Populates query parameters with required parameters. Such as
229 * _user_id, _api_key, etc.
230 * @param array $params
231 * @param integer $authType
232 */
233 private function getParams(array $params=array(), $authType=AuthType::BASIC) {
234 $params['_user_id'] = $this->userId;
235 $params['_api_key'] = $this->publicKey;
236
237 if (AuthType::BASIC == $authType) {
238
239 } else if (AuthType::HMAC == $authType) {
240 // Get current unix timestamp (UTC time).
241 $params['_timestamp'] = time();
242 // And calculate hash.
243 $params['_hash'] = $this->doHmac($params);
244 }
245
246 return $params;
247 }
248
249 private function doHmac(array $params) {
250 // Built-in 'http_build_query' function which is used
251 // to construct query string does not include parameters with null
252 // values which is incorrect in our case.
253 $params = array_map(function($x) { return is_null($x) ? '' : $x; }, $params);
254
255 // Sort query parameters by names using byte ordering.
256 // So 'param[10]' comes before 'param[2]'.
257 if (ksort($params)) {
258 // Url encode parameters. The encoding should be performed
259 // per RFC 1738 (http://www.faqs.org/rfcs/rfc1738)
260 // which implies that spaces are encoded as plus (+) signs.
261 $queryStr = http_build_query($params);
262 // Generate hash value based on encoded query string and
263 // secret key.
264 return hash_hmac(self::HMAC_ALGO, $queryStr, $this->secretKey);
265 } else {
266 throw new RuntimeException('Could not ksort data array');
267 }
268 }
269
270 public function prettyPrint( $json )
271 {
272 $result = '';
273 $level = 0;
274 $in_quotes = false;
275 $in_escape = false;
276 $ends_line_level = NULL;
277 $json_length = strlen( $json );
278
279 for( $i = 0; $i < $json_length; $i++ ) {
280 $char = $json[$i];
281 $new_line_level = NULL;
282 $post = "";
283 if( $ends_line_level !== NULL ) {
284 $new_line_level = $ends_line_level;
285 $ends_line_level = NULL;
286 }
287 if ( $in_escape ) {
288 $in_escape = false;
289 } else if( $char === '"' ) {
290 $in_quotes = !$in_quotes;
291 } else if( ! $in_quotes ) {
292 switch( $char ) {
293 case '}': case ']':
294 $level--;
295 $ends_line_level = NULL;
296 $new_line_level = $level;
297 break;
298
299 case '{': case '[':
300 $level++;
301 case ',':
302 $ends_line_level = $level;
303 break;
304
305 case ':':
306 $post = " ";
307 break;
308
309 case " ": case "\t": case "\n": case "\r":
310 $char = "";
311 $ends_line_level = $new_line_level;
312 $new_line_level = NULL;
313 break;
314 }
315 } else if ( $char === '\\' ) {
316 $in_escape = true;
317 }
318 if( $new_line_level !== NULL ) {
319 $result .= "\n".str_repeat( "\t", $new_line_level );
320 }
321 $result .= $char.$post;
322 }
323
324 return $result;
325 }
326}