-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCurlWrapper.php
69 lines (58 loc) · 1.44 KB
/
CurlWrapper.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?php
/**
* Created by Pavel Popov.
*/
namespace Metaer\CurlWrapperBundle;
class CurlWrapper implements CurlWrapperInterface
{
private $requestUrl;
private $requestBody;
private $responseBody;
/**
* @param array $curlOptions
* @return mixed
* @throws CurlWrapperException
*/
public function getQueryResult(array $curlOptions) {
$ch = curl_init();
foreach ($curlOptions as $key => $value) {
curl_setopt($ch, $key, $value);
if ($key == CURLOPT_URL) {
$this->requestUrl = $value;
} elseif ($key == CURLOPT_POSTFIELDS) {
$this->requestBody = $value;
}
}
$result = curl_exec($ch);
$curlErrorCode = curl_errno($ch);
$curlErrorText = curl_error($ch);
curl_close($ch);
if ($curlErrorCode) {
$errorMessage = sprintf('%s. Curl error code: %s', $curlErrorText, $curlErrorCode);
throw new CurlWrapperException($errorMessage);
}
$this->responseBody = $result;
return $result;
}
/**
* @return mixed
*/
public function getRequestUrl()
{
return $this->requestUrl;
}
/**
* @return mixed
*/
public function getRequestBody()
{
return $this->requestBody;
}
/**
* @return mixed
*/
public function getResponseBody()
{
return $this->responseBody;
}
}