forked from U-CRM/community
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsdk.php
More file actions
106 lines (81 loc) · 2.68 KB
/
sdk.php
File metadata and controls
106 lines (81 loc) · 2.68 KB
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
<?php
// Shared functions that can be used by all scripts.
define('TEMP_DIR', __DIR__ . '/temp');
class CurlException extends \Exception
{
}
function curlCommand($url, $method, array $headers = [], $data = null)
{
$c = curl_init();
curl_setopt($c, CURLOPT_URL, $url);
curl_setopt($c, CURLOPT_HTTPHEADER, $headers);
curl_setopt($c, CURLOPT_CUSTOMREQUEST, $method);
if ($data) {
curl_setopt($c, CURLOPT_POST, true);
curl_setopt($c, CURLOPT_POSTFIELDS, $data);
}
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
curl_setopt($c, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($c, CURLOPT_SSL_VERIFYHOST, 2);
$result = curl_exec($c);
$error = curl_error($c);
$errno = curl_errno($c);
if ($errno || $error) {
throw new CurlException("Error for request $url. Curl error $errno: $error");
}
$httpCode = curl_getinfo($c, CURLINFO_HTTP_CODE);
if ($httpCode < 200 || $httpCode >= 300) {
throw new CurlException("Error for request $url. HTTP error ($httpCode): $result", $httpCode);
}
curl_close($c);
}
function curlQuery($url, array $headers = [], array $parameters = [])
{
if ($parameters) {
$url .= '?' . http_build_query($parameters);
}
$c = curl_init();
curl_setopt($c, CURLOPT_URL, $url);
curl_setopt($c, CURLOPT_HTTPHEADER, $headers);
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
curl_setopt($c, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($c, CURLOPT_SSL_VERIFYHOST, 2);
$result = curl_exec($c);
$error = curl_error($c);
$errno = curl_errno($c);
if ($errno || $error) {
throw new CurlException("Error for request $url. Curl error $errno: $error");
}
$httpCode = curl_getinfo($c, CURLINFO_HTTP_CODE);
if ($httpCode < 200 || $httpCode >= 300) {
throw new CurlException("Error for request $url. HTTP error ($httpCode): $result", $httpCode);
}
curl_close($c);
if (! $result) {
throw new CurlException("Error for request $url. Empty result.");
}
return json_decode($result, true);
}
function ucrmApiCommand($endpoint, $method, array $data)
{
curlCommand(
sprintf('%s/api/v%s/%s', UCRM_API_URL, UCRM_API_VERSION, $endpoint),
$method,
[
'Content-Type: application/json',
'X-Auth-App-Key: ' . UCRM_API_KEY,
],
json_encode((object) $data)
);
}
function ucrmApiQuery($endpoint, array $parameters = [])
{
return curlQuery(
sprintf('%s/api/v%s/%s', UCRM_API_URL, UCRM_API_VERSION, $endpoint),
[
'Content-Type: application/json',
'X-Auth-App-Key: ' . UCRM_API_KEY,
],
$parameters
);
}