forked from jeremykendall/php-domain-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCurlHttpClient.php
More file actions
66 lines (58 loc) · 1.67 KB
/
Copy pathCurlHttpClient.php
File metadata and controls
66 lines (58 loc) · 1.67 KB
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
<?php
/**
* PHP Domain Parser: Public Suffix List based URL parsing.
*
* @see http://github.com/jeremykendall/php-domain-parser for the canonical source repository
*
* @copyright Copyright (c) 2017 Jeremy Kendall (http://jeremykendall.net)
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace Pdp;
final class CurlHttpClient implements HttpClient
{
/**
* @var array
*/
private $options;
/**
* new instance.
*
* @param array $options additional cURL options
*/
public function __construct(array $options = [])
{
$this->options = $options + [
CURLOPT_FAILONERROR => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_SSL_VERIFYHOST => false,
CURLOPT_HTTPGET => true,
];
$curl = curl_init();
$res = @curl_setopt_array($curl, $this->options);
curl_close($curl);
if (!$res) {
throw new Exception('Please verify your curl additionnal options');
}
}
/**
* {@inheritdoc}
*/
public function getContent(string $url): string
{
$curl = curl_init($url);
curl_setopt_array($curl, $this->options);
$content = curl_exec($curl);
$error_code = curl_errno($curl);
$error_message = curl_error($curl);
curl_close($curl);
if (CURLE_OK === $error_code) {
return $content;
}
throw new HttpClientException($error_message, $error_code);
}
}