Passed
Push — master ( e560bc...ee0cfd )
by Dev
02:02
created

Request::getError()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace PiedWeb\Curl;
4
5
class Request
6
{
7
    use UserAgentTrait;
8
9
    /**
10
     * Curl resource handle.
11
     *
12
     * @var resource
13
     */
14
    private $handle;
15
16
    /** @var string contains targeted URL */
17
    private $url;
18
19
    /** @var int */
20
    private $returnHeader = 0;
21
22
    /** @var mixed */
23
    private $filter;
24
25
    /**
26
     * Constructor.
27
     *
28
     * @param string $ur to request
29
     */
30 15
    public function __construct(?string $url = null)
31
    {
32 15
        $this->handle = curl_init();
33 15
        $this->setOpt(CURLOPT_RETURNTRANSFER, 1);
34
35 15
        if (null !== $url) {
36 15
            $this->setUrl($url);
37
        }
38 15
    }
39
40
    /**
41
     * Change the URL to cURL.
42
     *
43
     * @param string $url to request
44
     *
45
     * @return self
46
     */
47 15
    public function setUrl(string $url)
48
    {
49 15
        $this->url = $url;
50 15
        $this->setOpt(CURLOPT_URL, $url);
51
52 15
        return $this;
53
    }
54
55
    /**
56
     * Add a cURL's option.
57
     *
58
     * @param int   $option cURL Predefined Constant
59
     * @param mixed $value
60
     *
61
     * @return self
62
     */
63 15
    public function setOpt($option, $value)
64
    {
65 15
        curl_setopt($this->handle, $option, $value);
66
67 15
        return $this;
68
    }
69
70
    /**
71
     * A short way to set some classic options to cURL a web page.
72
     *
73
     * @return self
74
     */
75 15
    public function setDefaultGetOptions(
76
        $connectTimeOut = 5,
77
        $timeOut = 10,
78
        $dnsCacheTimeOut = 600,
79
        $followLocation = true,
80
        $maxRedirs = 5
81
    ) {
82
        $this
83 15
            ->setOpt(CURLOPT_AUTOREFERER, 1)
84 15
            ->setOpt(CURLOPT_FOLLOWLOCATION, $followLocation)
85 15
            ->setOpt(CURLOPT_MAXREDIRS, $maxRedirs)
86 15
            ->setOpt(CURLOPT_CONNECTTIMEOUT, $connectTimeOut)
87 15
            ->setOpt(CURLOPT_DNS_CACHE_TIMEOUT, $dnsCacheTimeOut)
88 15
            ->setOpt(CURLOPT_TIMEOUT, $timeOut)
89
             //->setOpt(CURLOPT_SSL_VERIFYPEER,    0);
90
        ;
91
92 15
        return $this;
93
    }
94
95
    /**
96
     * A short way to set some classic options to cURL a web page quickly
97
     * (but loosing some data like header, cookie...).
98
     *
99
     * @return self
100
     */
101 3
    public function setDefaultSpeedOptions(bool $cookie = false)
102
    {
103 3
        $this->setOpt(CURLOPT_SSL_VERIFYHOST, 0);
104 3
        $this->setOpt(CURLOPT_SSL_VERIFYPEER, 0);
105
106 3
        if (!$this->returnHeader) {
107 3
            $this->setOpt(CURLOPT_HEADER, 0);
108
        }
109 3
        if (!$cookie) {
110 3
            $this->setOpt(CURLOPT_COOKIE, 0);
111
        }
112 3
        $this->setDefaultGetOptions(5, 10, 600, true, 1);
113 3
        $this->setEncodingGzip();
114
115 3
        return $this;
116
    }
117
118
    /**
119
     * Call it if you want header informations.
120
     * After self::execute(), you would have this informations with getHeader();.
121
     *
122
     * @return self
123
     */
124 15
    public function setReturnHeader($only = false)
125
    {
126 15
        $this->setOpt(CURLOPT_HEADER, 1);
127 15
        $this->returnHeader = $only ? 2 : 1;
128
129 15
        if ($only) {
130
            $this->setOpt(CURLOPT_RETURNTRANSFER, 0);
131
            $this->setOpt(CURLOPT_NOBODY, 1);
132
        }
133
134 15
        return $this;
135
    }
136
137
    /**
138
     * An self::setOpt()'s alias to add a cookie to your request.
139
     *
140
     * @param string $cookie
141
     *
142
     * @return self
143
     */
144 3
    public function setCookie($cookie)
145
    {
146 3
        $this->setOpt(CURLOPT_COOKIE, $cookie);
147
148 3
        return $this;
149
    }
150
151
    /**
152
     * An self::setOpt()'s alias to add a referer to your request.
153
     *
154
     * @param string $referer
155
     *
156
     * @return self
157
     */
158 5
    public function setReferer($referer)
159
    {
160 5
        $this->setOpt(CURLOPT_REFERER, $referer);
161
162 5
        return $this;
163
    }
164
165
    /**
166
     * An self::setOpt()'s alias to add an user-agent to your request.
167
     *
168
     * @param string $ua
169
     *
170
     * @return self
171
     */
172 15
    public function setUserAgent($ua)
173
    {
174 15
        $this->setOpt(CURLOPT_USERAGENT, $ua);
175
176 15
        return $this;
177
    }
178
179
    /**
180
     * A short way to set post's options to cURL a web page.
181
     *
182
     * @param mixed $post if it's an array, will be converted via http build query
183
     *
184
     * @return self
185
     */
186
    public function setPost($post)
187
    {
188
        $this->setOpt(CURLOPT_CUSTOMREQUEST, 'POST');
189
        $this->setOpt(CURLOPT_POST, 1);
190
        $this->setOpt(CURLOPT_POSTFIELDS, is_array($post) ? http_build_query($post) : $post);
191
192
        return $this;
193
    }
194
195
    /**
196
     * If you want to request the URL and hope get the result gzipped.
197
     * The output will be automatically uncompress with execute();.
198
     *
199
     * @return self
200
     */
201 15
    public function setEncodingGzip()
202
    {
203 15
        $this->setOpt(CURLOPT_ENCODING, 'gzip, deflate');
204
205 15
        return $this;
206
    }
207
208
    /**
209
     * If you want to request the URL with a (http|socks...) proxy (public or private).
210
     *
211
     * @param string $proxy [scheme]IP:PORT[:LOGIN:PASSWORD]
212
     *                      Eg. : socks5://98.023.023.02:1098:cUrlRequestProxId:SecretPassword
213
     *
214
     * @return self
215
     */
216
    public function setProxy($proxy)
217
    {
218
        if (!empty($proxy)) {
219
            $scheme = Helper::getSchemeFrom($proxy);
220
            $proxy = explode(':', $proxy);
221
            $this->setOpt(CURLOPT_HTTPPROXYTUNNEL, 1);
222
            $this->setOpt(CURLOPT_PROXY, $scheme.$proxy[0].':'.$proxy[1]);
223
            if (isset($proxy[2])) {
224
                $this->setOpt(CURLOPT_PROXYUSERPWD, $proxy[2].':'.$proxy[3]);
225
            }
226
        }
227
228
        return $this;
229
    }
230
231
    /**
232
     * @param mixed $ContentType string or array
233
     *
234
     * @return self
235
     */
236 12
    public function setDownloadOnlyIf($ContentType = 'text/html')
237
    {
238 12
        $this->setReturnHeader();
239
240 12
        $this->filter = is_array($ContentType) ? $ContentType : [$ContentType];
241 12
        $this->setOpt(CURLOPT_HEADERFUNCTION, [$this, 'checkHeaderContentType']);
242 12
        $this->setOpt(CURLOPT_NOBODY, 1);
243
244 12
        return $this;
245
    }
246
247 6
    public function checkHeaderContentType($handle, $line)
248
    {
249 6
        if (is_string($line)) {
250 6
            foreach ($this->filter as $filter) {
251 6
                if (Helper::checkContentType($line, $filter)) {
252 6
                    $this->setOpt(CURLOPT_NOBODY, 0); //curl_setopt($handle, CURLOPT_NOBODY, 0);
253
                }
254
            }
255
        }
256
257 6
        return strlen($line);
258
    }
259
260
    /**
261
     * Execute the request.
262
     *
263
     * @return Response
264
     */
265 15
    public function exec()
266
    {
267 15
        $return = Response::get($this->handle, $this->url, $this->returnHeader);
268
269 15
        if ($return instanceof Response) {
270 2
            $this->setReferer($return->getEffectiveUrl());
271
        }
272
273 15
        return $return;
274
    }
275
276
    /**
277
     * Return the last error number (curl_errno).
278
     *
279
     * @return int the error number or 0 (zero) if no error occurred
280
     */
281
    public function hasError()
282
    {
283
        return curl_errno($this->handle);
284
    }
285
286
    /**
287
     * Return a string containing the last error for the current session (curl_error).
288
     *
289
     * @return string the error message or '' (the empty string) if no error occurred
290
     */
291 10
    public function getError()
292
    {
293 10
        return curl_error($this->handle);
294
    }
295
296
    /**
297
     * Get information regarding the request.
298
     *
299
     * @param int $opt This may be one of the following constants:
300
     *                 http://php.net/manual/en/function.curl-getinfo.php
301
     *
302
     * @return array|string|false
303
     */
304
    public function getInfo(?int $opt)
305
    {
306
        return $opt ? curl_getinfo($this->handle, $opt) : curl_getinfo($this->handle);
307
    }
308
309
    /**
310
     * Close the connexion
311
     * Call curl_reset function.
312
     */
313
    public function close()
314
    {
315
        curl_reset($this->handle);
316
    }
317
}
318