1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* PHP Domain Parser: Public Suffix List based URL parsing. |
7
|
|
|
* |
8
|
|
|
* @link http://github.com/jeremykendall/php-domain-parser for the canonical source repository |
9
|
|
|
* |
10
|
|
|
* @copyright Copyright (c) 2014 Jeremy Kendall (http://about.me/jeremykendall) |
11
|
|
|
* @license http://github.com/jeremykendall/php-domain-parser/blob/master/LICENSE MIT License |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Pdp\HttpAdapter; |
15
|
|
|
|
16
|
|
|
use Pdp\Exception\CurlHttpAdapterException; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* cURL http adapter. |
20
|
|
|
* |
21
|
|
|
* Lifted pretty much completely from William Durand's excellent Geocoder |
22
|
|
|
* project |
23
|
|
|
* |
24
|
|
|
* @link https://github.com/willdurand/Geocoder Geocoder on GitHub |
25
|
|
|
* |
26
|
|
|
* @author William Durand <[email protected]> |
27
|
|
|
* @author Jeremy Kendall <[email protected]> |
28
|
|
|
*/ |
29
|
|
|
class CurlHttpAdapter implements HttpAdapterInterface |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* {@inheritdoc} |
33
|
|
|
*/ |
34
|
4 |
|
public function getContent($url, $timeout = 5) |
35
|
|
|
{ |
36
|
|
|
// init |
37
|
4 |
|
$ch = curl_init(); |
38
|
|
|
|
39
|
4 |
|
if (false === $ch) { |
40
|
|
|
throw new \Exception('PHP-Domain-Parser: failed to initialize : cURL'); |
41
|
|
|
} |
42
|
|
|
|
43
|
4 |
|
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout); |
44
|
4 |
|
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); |
45
|
4 |
|
curl_setopt($ch, CURLOPT_URL, $url); |
46
|
4 |
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
47
|
4 |
|
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); |
48
|
4 |
|
curl_setopt($ch, CURLOPT_FAILONERROR, true); |
49
|
4 |
|
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); |
50
|
4 |
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); |
51
|
|
|
|
52
|
4 |
|
curl_setopt($ch, CURLOPT_USERAGENT, 'PHP-Domain-Parser cURL Request'); |
53
|
|
|
|
54
|
4 |
|
$content = curl_exec($ch); |
55
|
|
|
|
56
|
4 |
|
$errNo = curl_errno($ch); |
57
|
4 |
|
if ($errNo) { |
58
|
3 |
|
throw new CurlHttpAdapterException("CURL error [{$errNo}]: " . curl_error($ch), $errNo); |
59
|
|
|
} |
60
|
|
|
|
61
|
1 |
|
$responseCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
62
|
1 |
|
if ($responseCode !== 200) { |
63
|
|
|
throw new CurlHttpAdapterException('Wrong HTTP response code: ' . $responseCode, $responseCode); |
64
|
|
|
} |
65
|
|
|
|
66
|
1 |
|
curl_close($ch); |
67
|
|
|
|
68
|
1 |
|
return $content; |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|