1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Palmtree\Curl; |
4
|
|
|
|
5
|
|
|
use Palmtree\Curl\Exception\BadMethodCallException; |
6
|
|
|
use Palmtree\Curl\Exception\InvalidArgumentException; |
7
|
|
|
|
8
|
|
|
class Curl |
9
|
|
|
{ |
10
|
|
|
public static $defaultCurlOpts = [ |
11
|
|
|
CURLOPT_RETURNTRANSFER => true, |
12
|
|
|
CURLOPT_FOLLOWLOCATION => true, |
13
|
|
|
CURLOPT_AUTOREFERER => true, |
14
|
|
|
CURLOPT_USERAGENT => 'Palmtree\Curl', |
15
|
|
|
]; |
16
|
|
|
|
17
|
|
|
/** @var string */ |
18
|
|
|
private $url; |
19
|
|
|
/** @var resource */ |
20
|
|
|
private $handle; |
21
|
|
|
/** @var Request */ |
22
|
|
|
private $request; |
23
|
|
|
/** @var Response */ |
24
|
|
|
private $response; |
25
|
|
|
/** @var array */ |
26
|
|
|
private $curlOpts = []; |
27
|
|
|
|
28
|
|
|
const HTTP_NOT_FOUND = 404; |
29
|
|
|
const HTTP_OK_MIN = 200; |
30
|
|
|
const HTTP_OK_MAX = 299; |
31
|
|
|
|
32
|
2 |
|
public function __construct(string $url, array $curlOpts = []) |
33
|
|
|
{ |
34
|
2 |
|
$this->setUrl($url); |
|
|
|
|
35
|
|
|
|
36
|
|
|
$this->handle = \curl_init($url); |
|
|
|
|
37
|
|
|
$this->request = new Request(); |
38
|
|
|
|
39
|
|
|
$this->buildCurlOpts($curlOpts); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public static function getContents(string $url, array $curlOpts = []): string |
43
|
|
|
{ |
44
|
|
|
$curl = new self($url, $curlOpts); |
45
|
|
|
|
46
|
|
|
return $curl->getResponse()->getBody(); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param string|array $data |
51
|
|
|
*/ |
52
|
|
|
public function post($data): Response |
53
|
|
|
{ |
54
|
|
|
$this->getRequest()->setBody($data); |
55
|
|
|
|
56
|
|
|
return $this->execute(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function postJson($json): Response |
60
|
|
|
{ |
61
|
|
|
if (!\is_string($json)) { |
62
|
|
|
$json = \json_encode($json); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$this->getRequest()->addHeader('Content-Type', 'application/json'); |
66
|
|
|
$this->getRequest()->addHeader('Content-Length', \strlen($json)); |
67
|
|
|
|
68
|
|
|
return $this->post($json); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function execute(): Response |
72
|
|
|
{ |
73
|
|
|
if ($this->response !== null) { |
74
|
|
|
throw new BadMethodCallException('Request has already been executed'); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
if ($headers = $this->getRequest()->getHeaderStrings()) { |
78
|
|
|
\curl_setopt($this->handle, CURLOPT_HTTPHEADER, $headers); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
if ($body = $this->getRequest()->getBody()) { |
82
|
|
|
\curl_setopt($this->handle, CURLOPT_POST, true); |
83
|
|
|
\curl_setopt($this->handle, CURLOPT_POSTFIELDS, $body); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$response = \curl_exec($this->handle); |
87
|
|
|
$statusCode = \curl_getinfo($this->handle, CURLINFO_HTTP_CODE); |
88
|
|
|
|
89
|
|
|
$this->response = new Response($response, $statusCode); |
|
|
|
|
90
|
|
|
|
91
|
|
|
return $this->response; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function getRequest(): Request |
95
|
|
|
{ |
96
|
|
|
return $this->request; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function getResponse(): Response |
100
|
|
|
{ |
101
|
|
|
if ($this->response === null) { |
102
|
|
|
$this->execute(); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return $this->response; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function getUrl(): string |
109
|
|
|
{ |
110
|
|
|
return $this->url; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public function getOpts(): array |
114
|
|
|
{ |
115
|
|
|
return $this->curlOpts; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function setOpt(int $key, $value): self |
119
|
|
|
{ |
120
|
|
|
if ($key === CURLOPT_HEADER && !$value) { |
121
|
|
|
throw new InvalidArgumentException('CURLOPT_HEADER cannot be set to false'); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
$this->curlOpts[$key] = $value; |
125
|
|
|
|
126
|
|
|
\curl_setopt($this->handle, $key, $value); |
127
|
|
|
|
128
|
|
|
return $this; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
public function __toString(): string |
132
|
|
|
{ |
133
|
|
|
return $this->getResponse()->getBody() ?: ''; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
private function buildCurlOpts(array $curlOpts) |
137
|
|
|
{ |
138
|
|
|
$this->curlOpts = \array_replace(self::$defaultCurlOpts, $curlOpts); |
139
|
|
|
|
140
|
|
|
// The Response class always parses headers. |
141
|
|
|
$this->curlOpts[CURLOPT_HEADER] = true; |
142
|
|
|
|
143
|
|
|
\curl_setopt_array($this->handle, $this->curlOpts); |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.