1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: DrillCoder |
5
|
|
|
* Date: 2019-01-04 |
6
|
|
|
* Time: 01:13 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace DrillCoder\AmoCRM_Wrap; |
10
|
|
|
|
11
|
|
|
use DateTime; |
12
|
|
|
use DrillCoder\AmoCRM_Wrap\Helpers\Config; |
13
|
|
|
use stdClass; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class Base |
17
|
|
|
* @package DrillCoder\AmoCRM_Wrap |
18
|
|
|
*/ |
19
|
|
|
abstract class Base |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
protected static $domain; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
protected static $userLogin; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
protected static $userAPIKey; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var bool |
38
|
|
|
*/ |
39
|
|
|
protected static $authorization = false; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param string $url |
43
|
|
|
* @param array $data |
44
|
|
|
* @param DateTime|null $modifiedSince |
45
|
|
|
* @param bool $ajax |
46
|
|
|
* |
47
|
|
|
* @return stdClass|null |
48
|
|
|
* |
49
|
|
|
* @throws AmoWrapException |
50
|
|
|
*/ |
51
|
|
|
protected static function cUrl($url, $data = array(), DateTime $modifiedSince = null, $ajax = false) |
52
|
|
|
{ |
53
|
|
|
$url = 'https://' . self::$domain . '.amocrm.ru/' . $url; |
54
|
|
|
$isUnsorted = mb_stripos($url, 'incoming_leads') !== false; |
55
|
|
|
if ($isUnsorted) { |
56
|
|
|
$url .= '&login=' . self::$userLogin . '&api_key=' . self::$userAPIKey; |
57
|
|
|
} else { |
58
|
|
|
if (mb_strpos($url, '?') === false) { |
59
|
|
|
$url .= '?'; |
60
|
|
|
} |
61
|
|
|
$url .= '&USER_LOGIN=' . self::$userLogin . '&USER_HASH=' . self::$userAPIKey; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$curl = curl_init(); |
65
|
|
|
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); |
66
|
|
|
curl_setopt($curl, CURLOPT_URL, $url); |
67
|
|
|
curl_setopt($curl, CURLOPT_HEADER, false); |
68
|
|
|
curl_setopt($curl, CURLOPT_USERAGENT, 'DrillCoder AmoCRM_Wrap/v' . AmoCRM::VERSION); |
69
|
|
|
curl_setopt($curl, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2); |
70
|
|
|
$headers = array(); |
71
|
|
|
if (count($data) > 0) { |
72
|
|
|
curl_setopt($curl, CURLOPT_POST, true); |
73
|
|
|
if ($ajax) { |
74
|
|
|
$headers[] = 'X-Requested-With: XMLHttpRequest'; |
75
|
|
|
$dataStr = $data; |
76
|
|
|
} elseif ($isUnsorted) { |
77
|
|
|
$dataStr = http_build_query($data); |
78
|
|
|
} else { |
79
|
|
|
$headers[] = 'Content-Type: application/json'; |
80
|
|
|
$dataStr = json_encode($data); |
81
|
|
|
} |
82
|
|
|
curl_setopt($curl, CURLOPT_POSTFIELDS, $dataStr); |
83
|
|
|
} |
84
|
|
|
if ($modifiedSince !== null) { |
85
|
|
|
$headers[] = 'IF-MODIFIED-SINCE: ' . $modifiedSince->format(DateTime::RFC1123); |
86
|
|
|
} |
87
|
|
|
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); |
88
|
|
|
$json = curl_exec($curl); |
89
|
|
|
curl_close($curl); |
90
|
|
|
|
91
|
|
|
$result = json_decode($json); |
92
|
|
|
if (isset($result->response->error) || (isset($result->title) && $result->title === 'Error')) { |
93
|
|
|
$errorCode = isset($result->status) ? (int)$result->status : (int)$result->response->error_code; |
94
|
|
|
$errorMessage = isset(Config::$errors[$errorCode]) ? Config::$errors[$errorCode] : $result->response->error; |
95
|
|
|
|
96
|
|
|
throw new AmoWrapException($errorMessage, $errorCode); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return $result; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param string $var |
104
|
|
|
* |
105
|
|
|
* @return string |
106
|
|
|
*/ |
107
|
|
|
protected static function onlyNumbers($var) |
108
|
|
|
{ |
109
|
|
|
return preg_replace('/\D/', '', $var); |
110
|
|
|
} |
111
|
|
|
} |