-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAPIObject.php
130 lines (107 loc) · 3.34 KB
/
APIObject.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
<?php
/**
* Created by PhpStorm.
* User: ExE
* Date: 27.01.17
* Time: 12:00
*/
require_once "Pipefy.php";
class APIObject {
/**
* @param $url string
* @param $headers array
* @param $getData array
* @return mixed
* @throws Exception
*/
protected function send_get($url, $headers, $getData) {
$authHeaders = Pipefy::get_auth_headers();
$headers = array_merge($authHeaders, (array)$headers);
if ($getData != null)
$url .= "?" . http_build_query($getData);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
/**
* @param $url string
* @param $headers array
* @param $postData mixed
* @return mixed
* @throws Exception
*/
protected function send_post($url, $headers, $postData) {
$authHeaders = Pipefy::get_auth_headers();
$headers = array_merge($authHeaders, (array)$headers);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
/**
* @param $url string
* @param $headers array
* @param $putData mixed
* @return mixed
* @throws Exception
*/
protected function send_put($url, $headers, $putData) {
$putData = (is_array($putData)) ? http_build_query($putData) : $putData;
$authHeaders = Pipefy::get_auth_headers();
$headers = array_merge($authHeaders, (array)$headers, array('Content-Length: ' . strlen($putData)));
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, $putData);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
/**
* @param $resp mixed
*/
final protected function assign_results ($resp)
{
if (is_string($resp))
$resp = json_decode($resp);
foreach ($resp as $key => $val)
{
if (property_exists(get_called_class(), $key))
{
$this->$key = $val;
}
else {
$this->{$key} = $val;
}
}
}
/**
* @param $propName string
* @param $className string
*/
protected function parse_property($propName, $className) {
if (property_exists(get_called_class(), $propName) && $this->$propName != null) {
if (is_array($this->$propName)) {
$tmp_val = array();
foreach ($this->$propName as $key => $value) {
$tmp_val[] = new $className($value);
}
$this->$propName = $tmp_val;
}
else {
$this->$propName = new $className($this->$propName);
}
}
}
}