/
Curl.php
55 lines (43 loc) · 1.34 KB
/
Curl.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<?php
namespace Buzz\Client;
use Buzz\Message\MessageInterface;
use Buzz\Message\RequestInterface;
use Buzz\Exception\ClientException;
use Buzz\Exception\LogicException;
class Curl extends AbstractCurl implements ClientInterface
{
private $lastCurl;
public function send(RequestInterface $request, MessageInterface $response, array $options = array())
{
if (is_resource($this->lastCurl)) {
curl_close($this->lastCurl);
}
$this->lastCurl = static::createCurlHandle();
$this->prepare($this->lastCurl, $request, $options);
$data = curl_exec($this->lastCurl);
if (false === $data) {
$errorMsg = curl_error($this->lastCurl);
$errorNo = curl_errno($this->lastCurl);
throw new ClientException($errorMsg, $errorNo);
}
static::populateResponse($this->lastCurl, $data, $response);
}
/**
* Introspects the last cURL request.
*
* @see curl_getinfo()
*/
public function getInfo($opt = 0)
{
if (!is_resource($this->lastCurl)) {
throw new LogicException('There is no cURL resource');
}
return curl_getinfo($this->lastCurl, $opt);
}
public function __destruct()
{
if (is_resource($this->lastCurl)) {
curl_close($this->lastCurl);
}
}
}