|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace linkprofit\AmoCRM; |
|
4
|
|
|
use Exception; |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* Class RequestHandler |
|
8
|
|
|
* @package linkprofit\AmoCRM |
|
9
|
|
|
*/ |
|
10
|
|
|
class RequestHandler |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @var |
|
14
|
|
|
*/ |
|
15
|
|
|
protected $response; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var integer |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $httpCode; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var array |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $httpErrors = [ |
|
26
|
|
|
301 => 'Moved permanently', |
|
27
|
|
|
400 => 'Bad request', |
|
28
|
|
|
401 => 'Unauthorized', |
|
29
|
|
|
403 => 'Forbidden', |
|
30
|
|
|
404 => 'Not found', |
|
31
|
|
|
500 => 'Internal server error', |
|
32
|
|
|
502 => 'Bad gateway', |
|
33
|
|
|
503 => 'Service unavailable' |
|
34
|
|
|
]; |
|
35
|
|
|
|
|
36
|
|
|
protected $subdomain; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @param string $link |
|
40
|
|
|
* @param array $fields |
|
41
|
|
|
*/ |
|
42
|
3 |
|
public function performRequest($link, array $fields) |
|
43
|
|
|
{ |
|
44
|
3 |
|
$curl = curl_init(); |
|
45
|
|
|
|
|
46
|
3 |
|
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); |
|
47
|
3 |
|
curl_setopt($curl, CURLOPT_USERAGENT, 'amoCRM-API-client/1.0'); |
|
48
|
3 |
|
curl_setopt($curl, CURLOPT_URL, $link); |
|
49
|
3 |
|
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST'); |
|
50
|
3 |
|
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($fields)); |
|
51
|
3 |
|
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); |
|
52
|
3 |
|
curl_setopt($curl, CURLOPT_HEADER, false); |
|
53
|
3 |
|
curl_setopt($curl, CURLOPT_COOKIEFILE, $this->getCookiePath()); |
|
54
|
3 |
|
curl_setopt($curl, CURLOPT_COOKIEJAR, $this->getCookiePath()); |
|
55
|
3 |
|
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); |
|
56
|
3 |
|
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0); |
|
57
|
|
|
|
|
58
|
3 |
|
$this->response = curl_exec($curl); |
|
59
|
3 |
|
$this->httpCode = (int) curl_getinfo($curl, CURLINFO_HTTP_CODE); |
|
60
|
|
|
|
|
61
|
3 |
|
curl_close($curl); |
|
62
|
3 |
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @return bool |
|
66
|
|
|
*/ |
|
67
|
3 |
|
public function getResponse() |
|
68
|
|
|
{ |
|
69
|
3 |
|
if (!$this->response) { |
|
70
|
|
|
return false; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
3 |
|
$this->encodeResponse(); |
|
74
|
|
|
|
|
75
|
3 |
|
return $this->response; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @param $subdomain string |
|
80
|
|
|
*/ |
|
81
|
4 |
|
public function setSubdomain($subdomain) |
|
82
|
|
|
{ |
|
83
|
4 |
|
$this->subdomain = $subdomain; |
|
84
|
4 |
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @return mixed |
|
88
|
|
|
*/ |
|
89
|
1 |
|
public function getSubdomain() |
|
90
|
|
|
{ |
|
91
|
1 |
|
return $this->subdomain; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Encoding response from json, throw exception in case of wrong http code |
|
96
|
|
|
*/ |
|
97
|
3 |
|
protected function encodeResponse() |
|
98
|
|
|
{ |
|
99
|
|
|
try { |
|
100
|
3 |
|
if ($this->httpCode != 200 && $this->httpCode != 204) { |
|
101
|
2 |
|
throw new Exception(isset($this->httpErrors[$this->httpCode]) ? $this->httpErrors[$this->httpCode] : 'Undescribed error', $this->httpCode); |
|
102
|
|
|
} |
|
103
|
3 |
|
} catch (Exception $e) { |
|
104
|
2 |
|
echo 'Error: ' . $e->getMessage() . PHP_EOL . 'Error code: ' . $e->getCode(); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
3 |
|
$this->response = json_decode($this->response, true); |
|
108
|
3 |
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* @return string |
|
112
|
|
|
*/ |
|
113
|
3 |
|
protected function getCookiePath() |
|
114
|
|
|
{ |
|
115
|
3 |
|
return dirname(dirname(__FILE__)) . '/cookie.txt'; |
|
116
|
|
|
} |
|
117
|
|
|
} |