Completed
Push — master ( 9cb038...08e890 )
by Dev
02:40
created

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