forked from Chive/pipelinedeals-php-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
class.php
executable file
·81 lines (61 loc) · 2.35 KB
/
class.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
<?php
/* Author:
chive@github
Kim Thoenen
smuzey Web Design & Development
www.smuzey.ch
*/
class PDAdapter {
private $api_key;
protected $base_url = "https://api.pipelinedeals.com/api/v3/";
protected $method = "get";
protected $request_url;
protected $request_data;
public function __construct($api_key, $custom_base_url = null) {
$this->api_key = $api_key;
if (!empty($custom_base_url)) { $this->base_url = $custom_base_url; }
}
public function setMethod($method) {
$this->method = $method;
}
public function doRequest($res, $conditions = null, $page = null, $data = null) {
$additional_params = null;
// Adding condition params to URL
if (!empty($conditions)) {
foreach ($conditions as $key=>$value) {
$additional_params .= "&conditions[" . $key . "]=" . $value;
}
}
// Adding page param to URL
if (!empty($page)) { $additional_params .= "&page=" . $page; }
if (!empty($data)) { $this->request_data = $data; }
$this->constructURL($res,$additional_params);
return $this->decodeJSON($this->performCURL());
}
private function constructURL($res,$additional = "") {
$this->request_url = $this->base_url . $res . ".json?api_key=" . $this->api_key . $additional;
}
private function performCURL() {
$ch = curl_init();
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_COOKIESESSION,0);
curl_setopt($ch, CURLOPT_COOKIEFILE, dirname(__FILE__) . "/cookie.txt");
curl_setopt($ch, CURLOPT_COOKIEJAR, dirname(__FILE__) . "/cookie.txt");
curl_setopt($ch, CURLOPT_URL, $this->request_url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
if ($this->method == "post" OR $this->method == "put" OR $this->method == "delete") {
if ($this->method == "post") { curl_setopt($ch, CURLOPT_POST, 1); }
else if ($this->method == "put") { curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT"); }
else if ($this->method == "delete") { curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE"); }
curl_setopt($ch, CURLOPT_POSTFIELDS, $this->request_data);
}
$res = curl_exec($ch);
curl_close($ch);
return $res;
}
private function decodeJSON ($res) {
return json_decode((string)$res,true);
}
}