-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathBase.php
111 lines (99 loc) · 3.11 KB
/
Base.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
<?php
/**
* Created by PhpStorm.
* User: DrillCoder
* Date: 2019-01-04
* Time: 01:13
*/
namespace DrillCoder\AmoCRM_Wrap;
use DateTime;
use DrillCoder\AmoCRM_Wrap\Helpers\Config;
use stdClass;
/**
* Class Base
* @package DrillCoder\AmoCRM_Wrap
*/
abstract class Base
{
/**
* @var string
*/
protected static $domain;
/**
* @var string
*/
protected static $userLogin;
/**
* @var string
*/
protected static $userAPIKey;
/**
* @var bool
*/
protected static $authorization = false;
/**
* @param string $url
* @param array $data
* @param DateTime|null $modifiedSince
* @param bool $ajax
*
* @return stdClass|null
*
* @throws AmoWrapException
*/
protected static function cUrl($url, $data = array(), DateTime $modifiedSince = null, $ajax = false)
{
$url = 'https://' . self::$domain . '.amocrm.ru/' . $url;
$isUnsorted = mb_stripos($url, 'incoming_leads') !== false;
if ($isUnsorted) {
$url .= '&login=' . self::$userLogin . '&api_key=' . self::$userAPIKey;
} else {
if (mb_strpos($url, '?') === false) {
$url .= '?';
}
$url .= '&USER_LOGIN=' . self::$userLogin . '&USER_HASH=' . self::$userAPIKey;
}
$curl = curl_init();
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_USERAGENT, 'DrillCoder AmoCRM_Wrap/v' . AmoCRM::VERSION);
curl_setopt($curl, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2);
$headers = array();
if (count($data) > 0) {
curl_setopt($curl, CURLOPT_POST, true);
if ($ajax) {
$headers[] = 'X-Requested-With: XMLHttpRequest';
$dataStr = $data;
} elseif ($isUnsorted) {
$dataStr = http_build_query($data);
} else {
$headers[] = 'Content-Type: application/json';
$dataStr = json_encode($data);
}
curl_setopt($curl, CURLOPT_POSTFIELDS, $dataStr);
}
if ($modifiedSince !== null) {
$headers[] = 'IF-MODIFIED-SINCE: ' . $modifiedSince->format(DateTime::RFC1123);
}
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
$json = curl_exec($curl);
curl_close($curl);
$result = json_decode($json);
if (isset($result->response->error) || (isset($result->title) && $result->title === 'Error')) {
$errorCode = isset($result->status) ? (int)$result->status : (int)$result->response->error_code;
$errorMessage = isset(Config::$errors[$errorCode]) ? Config::$errors[$errorCode] : $result->response->error;
throw new AmoWrapException($errorMessage, $errorCode);
}
return $result;
}
/**
* @param string $var
*
* @return string
*/
protected static function onlyNumbers($var)
{
return preg_replace('/\D/', '', $var);
}
}