-
Notifications
You must be signed in to change notification settings - Fork 0
/
incentibox.php
146 lines (124 loc) · 4.06 KB
/
incentibox.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
<?php
/**
* Sample PHP API Client
* @author Andrew Waage
* @created 2011-05-26
* @version 1.0
* @copyright Copyright (c) 2011, Andrew Waage. All rights reserved.
* @license Apache License V.2.0
*/
class Incentibox {
const DEBUG = false;
const INCENTIBOX_API_URL = 'http://incentibox.dev/api'; //'http://api.incentibox.com';
const INCENTIBOX_API_PORT = 80;
const API_VERSION = '1';
private $password;
private $time_out = 60;
private $user_agent;
private $username;
// class methods
public function __construct($username = null, $password = null) {
if($username !== null) $this->set_username($username);
if($password !== null) $this->set_password($password);
}
private function perform_call($url, $params = array(), $authenticate = false, $use_post = true) {
// redefine
$url = (string) $url;
$aParameters = (array) $params;
$authenticate = (bool) $authenticate;
$use_post = (bool) $use_post;
// build url
$url = self::INCENTIBOX_API_URL .'/v'. self::API_VERSION . '/' . $url;
// validate needed authentication
if($authenticate && ($this->get_username() == '' || $this->get_password() == '')) {
throw new IncentiboxException('No username or password was set.');
}
// build GET URL if not using post
if(!empty($params) && !$use_post){
$url .= '?'. http_build_query( $params );
}
// set options
$options[CURLOPT_URL] = $url;
$options[CURLOPT_PORT] = self::INCENTIBOX_API_PORT;
$options[CURLOPT_USERAGENT] = $this->get_useragent();
$options[CURLOPT_FOLLOWLOCATION] = true;
$options[CURLOPT_RETURNTRANSFER] = true;
$options[CURLOPT_TIMEOUT] = (int) $this->get_time_out();
// HTTP basic auth
if($authenticate) {
$options[CURLOPT_HTTPAUTH] = CURLAUTH_BASIC;
$options[CURLOPT_USERPWD] = $this->get_username() .':'. $this->get_password();
}
// build post params if $use_post
if(!empty($params) && $use_post) {
$options[CURLOPT_POST] = true;
$options[CURLOPT_POSTFIELDS] = http_build_query( $params );
}
// curl init
$curl = curl_init();
// set options
curl_setopt_array($curl, $options);
// execute
$response = curl_exec($curl);
$headers = curl_getinfo($curl);
// fetch errors and status code
$errorNumber = curl_errno($curl);
$errorMessage = curl_error($curl);
$http_status_code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
// close
curl_close($curl);
return array('response_code' => $http_status_code,
'response' => $response);
}
// Getters
private function get_password(){
return (string) $this->password;
}
public function get_time_out(){
return (int) $this->time_out;
}
public function get_useragent(){
return (string) 'PHP IncentiBox API Client/'. self::API_VERSION .' '. $this->user_agent;
}
private function get_username(){
return (string) $this->username;
}
// Setters
private function set_username($username){
$this->username = (string) $username;
}
private function set_password($password){
$this->password = (string) $password;
}
public function set_time_out($seconds){
$this->time_out = (int) $seconds;
}
public function set_user_agent($user_agent){
$this->user_agent = (string) $user_agent;
}
/**
* Returns all redeemed_rewards for the given program_id.
* If param @last_redeemed_reward_id is given, returns all redeemed_rewards where id > @last_redeemed_reward_id
*
* @return array
* @param $program_id
* @param $last_redeemed_reward_id (optional)
*/
public function get_redeemed_rewards($program_id, $last_redeemed_reward_id = null) {
// build url
$url = 'programs/'.urlencode($program_id).'/redeemed_rewards';
$url = ($last_redeemed_reward_id != null) ? $url . '/' . urlencode($last_redeemed_reward_id) : $url;
$response = $this->perform_call($url, array(), true, false);
$response_code = $response['response_code'];
$response = $response['response'];
// decode the returned json
if ($response_code == 200){
return json_decode($response,true);
} else {
throw new IncentiboxException($response_code . ' - ' . $response);
}
}
}
// IncentiboxException
class IncentiboxException extends Exception { }
?>