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
|
|
|
* CurlHandle destructor. |
29
|
|
|
*/ |
30
|
13 |
|
public function __destruct() |
31
|
|
|
{ |
32
|
13 |
|
if ($this->handle !== null) { |
33
|
|
|
@curl_close($this->handle); |
|
|
|
|
34
|
|
|
} |
35
|
13 |
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Open cURL handle. |
39
|
|
|
* |
40
|
|
|
* @throws NetworkException |
41
|
|
|
* |
42
|
|
|
* @return resource |
43
|
|
|
*/ |
44
|
3 |
|
public function open() |
45
|
|
|
{ |
46
|
3 |
|
if ($this->handle !== null) { |
47
|
1 |
|
return $this->handle; |
48
|
|
|
} |
49
|
|
|
|
50
|
3 |
|
if (!function_exists('curl_init')) { |
51
|
|
|
throw new NetworkException('The cURL PHP extension was not loaded.'); |
52
|
|
|
} |
53
|
3 |
|
$this->handle = curl_init(); |
54
|
|
|
|
55
|
3 |
|
return $this->handle; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Close cURL handle. |
60
|
|
|
*/ |
61
|
3 |
|
public function close() |
62
|
|
|
{ |
63
|
3 |
|
if ($this->handle === null) { |
64
|
1 |
|
return; |
65
|
|
|
} |
66
|
|
|
|
67
|
2 |
|
curl_setopt($this->handle, CURLOPT_HEADERFUNCTION, null); |
68
|
2 |
|
curl_setopt($this->handle, CURLOPT_READFUNCTION, null); |
69
|
2 |
|
curl_setopt($this->handle, CURLOPT_WRITEFUNCTION, null); |
70
|
2 |
|
curl_setopt($this->handle, CURLOPT_PROGRESSFUNCTION, null); |
71
|
2 |
|
curl_reset($this->handle); |
72
|
2 |
|
} |
73
|
|
|
} |
74
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: