-
Notifications
You must be signed in to change notification settings - Fork 0
/
producteev.php
230 lines (206 loc) · 6.7 KB
/
producteev.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
228
229
230
<?php
/**
* Author: Angelo R.
* Link: http://xangelo.ca
*
* Modified by: Nick Verwymeren
* Link: http://makesomecode.com
* Version: 1.0
*
* This class allows you to access the Producteev api through PHP. It's very simple but currently only supports
* JSON resultsets.
*
* You'll find examples at the bottom
*
* Thanks for downloading, I've provided this as part of the MIT licensing agreement so that you are pretty much
* free to do whatever you want with it.
*
*/
class Producteev {
private $api_key;
private $api_secret;
private $base_url;
private $return_type;
private $params;
private $token;
private $dump;
/**
* Basically, you can either set it here if you only make occasional calls to the api, or (preferred method)
* just set it directly in the class by modifying $api_key and $api_secret directly. That way you're not
* setting it as part of every instantiation.
* @param string $key your api key
* @param string $secret your api secret
*/
public function __construct($key = '',$secret = '') {
if($this->api_key == null) {
if($key == '') {
die('Producteev API Key not set');
}
else {
$this->api_key = $key;
}
}
if($this->api_secret == null) {
if($secret == '') {
die('Producteev API Secret not set');
}
else {
$this->api_secret = $secret;
}
}
$this->base_url = 'https://api.producteev.com/';
$this->params = array();
$this->needed_params = array();
$this->token = null;
}
/**
* At the moment it only supports json, so there really isn't a need to even USE this. The Execute() method
* will default to json anyways.
* @param string $type one of json |
* @return void
*/
public function SetReturnType($type) {
if(in_array($type,array('json'))) {
$this->return_type = $type;
}
else {
die('Invalid request type');
}
}
/**
* This method is used to set any variable information. So, something that would change on every request would
* go here. After every request is made this is wiped clean to prepare for the next request.
* @param string
* @param value
* @return void
*/
public function Set($key,$val = '') {
if(is_array($key)) {
foreach($key as $k=>$v) {
$this->Set($k,$v);
}
}
else {
$this->params[$key] = $val;
}
}
public function Get($key) {
return $this->params[$key];
}
/**
* Logs in
* @param string $email
* @param string $password
* @return stdClass
*/
public function Login($email,$password) {
$this->Set('email',$email);
$this->Set('password',$password);
$d = $this->Execute('users/login');
$this->token = $d->login->token;
return $d;
}
/**
* Executes whatever action is passed to it.
*
* This method ensures that the url is properly formatted. It doesn't actually perform an direct operation on
* the url, instead it will call $this->Curl() which will actually perform the request and deal with the
* results.
* @param string $action One of the supported actions from the api
* @return mixed An stdObject containing the results of your request
*/
public function Execute($action) {
$url = $this->base_url.$action;
$url .= ($this->return_type == null)?'.json?':'.'.$this->return_type.'?';
$url .= $this->GenerateUrl();
return $this->Curl($url);
}
/**
* At the moment this just performs the request and then returns the data (after storing the token if there
* is one)
* @param $url
* @return mixed
*/
private function Curl($url){
$ch = curl_init($url);
curl_setopt_array($ch, array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false, // Shortcut for now
));
$result = curl_exec($ch);
$data = json_decode($result);
switch(curl_getinfo($ch,CURLINFO_HTTP_CODE)) {
case 200:
// Patch to ensure that the token is always stored
if($data->login != null && $data->login->token != null) {
$this->token = $data->login->token;
}
break;
case 403:
echo $url.'<br>';
echo $data->error->message.'<br>';
echo '<pre>'.print_r($this->dump,true).'</pre>';
break;
default:
die('<div>'.curl_error($ch).'</div>');
}
curl_close($ch);
$this->ClearParams();
return $data;
}
/**
* Just deletes the params for now.
* @return void
*/
public function ClearParams() {
$this->params = array();
}
/**
* Generates the URL through concatination of params and persistent params. It generates the signature first
* and then proceeds to mush all the parameters together.
* @return string
*/
private function GenerateUrl() {
$this->Set('api_key',$this->api_key);
if($this->token != null) {
$this->Set('token',$this->token);
}
// Needs to be called after all params are set
$this->GenerateSignature();
$str = '';
// Creates the final query string
foreach($this->params as $x=>$d) {
$str .= $x.'='.urlencode($d).'&';
}
return substr($str,0,strlen($str)-1);
}
/**
* Essentially the same code that is present on the api website, this just constructs the signature
* @return void
*/
public function GenerateSignature() {
$str = '';
ksort($this->params); // THIS IS VITAL!
foreach ($this->params as $k=>$v) {
if(is_string($v)) {
$str .= "$k$v";
}
}
$str .= $this->api_secret;
$str = stripslashes($str);
$this->Set('api_sig',md5($str));
}
}
/*
* Example:
* $p = new Producteev('Your key','Your secret');
* $p->SetReturnType('json');
* $p->Set(array(
* 'email'=>'[email protected]',
* 'password'=>'08374f')
* );
* $data = $p->Execute('users/login');
* $data = $p->Execute('dashboards/show_list');
*
* echo '<pre>'.print_r($data,true).'</pre>';
*/