|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace ddlzz\AmoAPI\Request; |
|
5
|
|
|
use ddlzz\AmoAPI\Exceptions\ErrorCodeException; |
|
6
|
|
|
use ddlzz\AmoAPI\Exceptions\FailedAuthException; |
|
7
|
|
|
use ddlzz\AmoAPI\SettingsStorage; |
|
8
|
|
|
|
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Class DataSender. It's responsible for interacting with amoCRM. |
|
12
|
|
|
* @package ddlzz\AmoAPI |
|
13
|
|
|
* @author ddlzz |
|
14
|
|
|
*/ |
|
15
|
|
|
class DataSender |
|
16
|
|
|
{ |
|
17
|
|
|
/** @var SettingsStorage */ |
|
18
|
|
|
private $settings; |
|
19
|
|
|
|
|
20
|
|
|
/** @var Curl */ |
|
21
|
|
|
private $curl; |
|
22
|
|
|
|
|
23
|
|
|
/** DataSender constructor. |
|
24
|
|
|
* @param Curl $curl |
|
25
|
|
|
* @param SettingsStorage $settings |
|
26
|
|
|
*/ |
|
27
|
4 |
|
public function __construct(Curl $curl, SettingsStorage $settings) |
|
28
|
|
|
{ |
|
29
|
4 |
|
$this->curl = $curl; |
|
30
|
4 |
|
$this->settings = $settings; |
|
31
|
4 |
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @param string $url |
|
35
|
|
|
* @param array $data |
|
36
|
|
|
* @return string |
|
37
|
|
|
* @throws ErrorCodeException |
|
38
|
|
|
* @throws FailedAuthException |
|
39
|
|
|
* @throws \ddlzz\AmoAPI\Exceptions\CurlException |
|
40
|
|
|
*/ |
|
41
|
3 |
|
public function send($url, array $data = []) |
|
42
|
|
|
{ |
|
43
|
3 |
|
$this->curl->init(); |
|
44
|
|
|
|
|
45
|
3 |
|
$this->curl->setReturnTransfer(true); |
|
46
|
3 |
|
$this->curl->setUserAgent($this->settings->getUserAgent()); |
|
47
|
3 |
|
$this->curl->setUrl($url); |
|
48
|
3 |
|
$this->curl->setHttpHeader([$this->settings::SENDER_HTTP_HEADER]); |
|
49
|
3 |
|
$this->curl->setHeader(false); |
|
50
|
3 |
|
$this->curl->setCookieFile($this->settings->getCookiePath()); |
|
51
|
3 |
|
$this->curl->setCookieJar($this->settings->getCookiePath()); |
|
52
|
3 |
|
$this->curl->setSSLVerifyPeer(false); |
|
53
|
3 |
|
$this->curl->setSSLVerifyHost(2); |
|
54
|
|
|
|
|
55
|
3 |
|
if (!empty($data)) { |
|
56
|
3 |
|
$this->curl->setCustomRequest('POST'); |
|
57
|
3 |
|
$this->curl->setPostFields(json_encode($data)); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
3 |
|
$response = $this->curl->exec(); |
|
61
|
3 |
|
$code = $this->curl->getHttpCode(); |
|
62
|
|
|
|
|
63
|
3 |
|
$this->curl->close(); |
|
64
|
3 |
|
$this->validateCode($code, $url, $response); |
|
|
|
|
|
|
65
|
|
|
|
|
66
|
1 |
|
return (string)$response; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @param int $code |
|
71
|
|
|
* @return string |
|
72
|
|
|
*/ |
|
73
|
2 |
|
private static function getErrorByHttpCode($code) |
|
74
|
|
|
{ |
|
75
|
|
|
$errors = [ |
|
76
|
2 |
|
301 => '301 Moved permanently', |
|
77
|
|
|
400 => '400 Bad request', |
|
78
|
|
|
401 => '401 Unauthorized or Blocked', |
|
79
|
|
|
403 => '403 Forbidden', |
|
80
|
|
|
404 => '404 Not found', |
|
81
|
|
|
429 => '429 Too Many Requests', |
|
82
|
|
|
500 => '500 Internal server error', |
|
83
|
|
|
502 => '502 Bad gateway', |
|
84
|
|
|
503 => '503 Service unavailable', |
|
85
|
|
|
504 => '504 Gateway Timeout', |
|
86
|
|
|
]; |
|
87
|
|
|
|
|
88
|
2 |
|
return isset($errors[$code]) ? $errors[$code] : $code . ' Unknown error'; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @param int $code |
|
93
|
|
|
* @param string $url |
|
94
|
|
|
* @param string $response |
|
95
|
|
|
* @return bool |
|
96
|
|
|
* @throws ErrorCodeException |
|
97
|
|
|
* @throws FailedAuthException |
|
98
|
|
|
*/ |
|
99
|
3 |
|
private function validateCode($code, $url, $response) |
|
100
|
|
|
{ |
|
101
|
3 |
|
if ((401 === $code) || (403 === $code)) { |
|
102
|
1 |
|
throw new FailedAuthException('Auth failed! ' . self::getErrorByHttpCode($code), $response); |
|
103
|
2 |
|
} elseif ((200 !== $code) && (204 !== $code)) { |
|
104
|
1 |
|
throw new ErrorCodeException(self::getErrorByHttpCode($code), $response, $url); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
1 |
|
return true; |
|
108
|
|
|
} |
|
109
|
|
|
} |