|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace AmoCRM\Request; |
|
4
|
|
|
|
|
5
|
|
|
use AmoCRM\NetworkException; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Class CurlHandle |
|
9
|
|
|
* |
|
10
|
|
|
* Класс, хранящий повторно используемый обработчик cURL |
|
11
|
|
|
* |
|
12
|
|
|
* @package AmoCRM\Request |
|
13
|
|
|
* @author dotzero <[email protected]> |
|
14
|
|
|
* @link http://www.dotzero.ru/ |
|
15
|
|
|
* @link https://github.com/dotzero/amocrm-php |
|
16
|
|
|
* |
|
17
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
18
|
|
|
* file that was distributed with this source code. |
|
19
|
|
|
*/ |
|
20
|
|
|
class CurlHandle |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* @var resource Повторно используемый обработчик cURL |
|
24
|
|
|
*/ |
|
25
|
|
|
private $handle; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Закрывает обработчик cURL |
|
29
|
|
|
*/ |
|
30
|
13 |
|
public function __destruct() |
|
31
|
|
|
{ |
|
32
|
13 |
|
if ($this->handle !== null) { |
|
33
|
|
|
@curl_close($this->handle); |
|
|
|
|
|
|
34
|
|
|
} |
|
35
|
13 |
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Возвращает повторно используемый обработчик cURL или создает новый |
|
39
|
|
|
* |
|
40
|
|
|
* @return resource |
|
41
|
|
|
* @throws NetworkException |
|
42
|
|
|
*/ |
|
43
|
3 |
|
public function open() |
|
44
|
|
|
{ |
|
45
|
3 |
|
if ($this->handle !== null) { |
|
46
|
1 |
|
return $this->handle; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
3 |
|
if (!function_exists('curl_init')) { |
|
50
|
|
|
throw new NetworkException('The cURL PHP extension was not loaded.'); |
|
51
|
|
|
} |
|
52
|
3 |
|
$this->handle = curl_init(); |
|
53
|
|
|
|
|
54
|
3 |
|
return $this->handle; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Сбрасывает настройки обработчика cURL |
|
59
|
|
|
*/ |
|
60
|
3 |
|
public function close() |
|
61
|
|
|
{ |
|
62
|
3 |
|
if ($this->handle === null) { |
|
63
|
1 |
|
return; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
2 |
|
curl_setopt($this->handle, CURLOPT_HEADERFUNCTION, null); |
|
67
|
2 |
|
curl_setopt($this->handle, CURLOPT_READFUNCTION, null); |
|
68
|
2 |
|
curl_setopt($this->handle, CURLOPT_WRITEFUNCTION, null); |
|
69
|
2 |
|
curl_setopt($this->handle, CURLOPT_PROGRESSFUNCTION, null); |
|
70
|
2 |
|
curl_reset($this->handle); |
|
71
|
2 |
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: