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