-
Notifications
You must be signed in to change notification settings - Fork 4
/
Http.php
54 lines (44 loc) · 1.26 KB
/
Http.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
<?php
namespace Camillebaronnet\ETL\Extractor;
use Camillebaronnet\ETL\Exception\MissingParameter;
class Http extends AbstractExtractor
{
public $method = 'GET';
public $url;
public $data;
public $curlOpts = [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_USERAGENT => '-',
CURLOPT_AUTOREFERER => true,
CURLOPT_SSL_VERIFYHOST => 2,
CURLOPT_SSL_VERIFYPEER => true,
];
/**
* @throws MissingParameter
*/
public function __invoke(): iterable
{
$this->requiredParameters(['url']);
$curl_opts = [
CURLOPT_URL => $this->url,
CURLOPT_CUSTOMREQUEST => strtoupper($this->method),
];
if (null !== $this->data) {
$curl_opts += [
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $this->data,
];
}
if ('HEAD' === strtoupper($this->method)) {
$curl_opts += [CURLOPT_NOBODY => 1];
}
$ch = curl_init();
curl_setopt_array($ch, $curl_opts + $this->curlOpts);
yield [
'body' => curl_exec($ch),
'contentType' => curl_getinfo($ch, CURLINFO_CONTENT_TYPE)
];
curl_close($ch);
}
}