|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Rss/Atom Bundle for Symfony. |
|
5
|
|
|
* |
|
6
|
|
|
* |
|
7
|
|
|
* @license http://opensource.org/licenses/lgpl-3.0.html LGPL |
|
8
|
|
|
* @copyright (c) 2013, Alexandre Debril |
|
9
|
|
|
*/ |
|
10
|
|
|
namespace Debril\RssAtomBundle\Driver; |
|
11
|
|
|
|
|
12
|
|
|
use Debril\RssAtomBundle\Exception\DriverUnreachableResourceException; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Class HttpCurlDriver. |
|
16
|
|
|
*/ |
|
17
|
|
|
class HttpCurlDriver implements HttpDriverInterface |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* Configuration options |
|
21
|
|
|
* @var array |
|
22
|
|
|
*/ |
|
23
|
|
|
private $options; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Constructor for passing config options |
|
27
|
|
|
* @param array $options |
|
28
|
|
|
*/ |
|
29
|
1 |
|
public function __construct($options = array()) { |
|
30
|
|
|
|
|
31
|
1 |
|
$this->options = $options; |
|
32
|
|
|
|
|
33
|
1 |
|
$defaults = array('timeout' => 10, |
|
34
|
1 |
|
'useragent' => 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.2) Gecko/20090729 Firefox/3.5.2 GTB5', |
|
35
|
1 |
|
'maxredirs' => 5); |
|
36
|
|
|
|
|
37
|
1 |
|
foreach ( $defaults as $key => $value ) { |
|
38
|
1 |
|
if ( !isset( $this->options[$key]) ) { |
|
39
|
1 |
|
$this->options[$key] = $value; |
|
40
|
1 |
|
} |
|
41
|
1 |
|
} |
|
42
|
1 |
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @param string $url |
|
46
|
|
|
* @param \DateTime $lastModified |
|
47
|
|
|
* |
|
48
|
|
|
* @return \Debril\RssAtomBundle\Driver\HttpDriverResponse |
|
49
|
|
|
* |
|
50
|
|
|
* @throws DriverUnreachableResourceException |
|
51
|
|
|
*/ |
|
52
|
2 |
|
public function getResponse($url, \DateTime $lastModified) |
|
53
|
|
|
{ |
|
54
|
2 |
|
$curl = curl_init($url); |
|
55
|
2 |
|
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); |
|
56
|
2 |
|
curl_setopt($curl, CURLOPT_HEADER, true); |
|
57
|
2 |
|
curl_setopt($curl, CURLOPT_TIMECONDITION, CURL_TIMECOND_IFMODSINCE); |
|
58
|
2 |
|
curl_setopt($curl, CURLOPT_USERAGENT, $this->options['useragent']); |
|
59
|
2 |
|
curl_setopt($curl, CURLOPT_TIMEVALUE, $lastModified->getTimestamp()); |
|
60
|
2 |
|
curl_setopt($curl, CURLOPT_TIMEOUT, $this->options['timeout']); |
|
61
|
2 |
|
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); |
|
62
|
2 |
|
curl_setopt($curl, CURLOPT_MAXREDIRS, $this->options['maxredirs']); |
|
63
|
2 |
|
$curlReturn = curl_exec($curl); |
|
64
|
|
|
|
|
65
|
2 |
|
if (!$curlReturn) { |
|
66
|
1 |
|
$err = curl_error($curl); |
|
67
|
1 |
|
throw new DriverUnreachableResourceException("Error accessing {$url} : {$err}"); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
1 |
|
$headerSize = curl_getinfo($curl, CURLINFO_HEADER_SIZE); |
|
71
|
1 |
|
curl_close($curl); |
|
72
|
|
|
|
|
73
|
1 |
|
return $this->getHttpResponse( |
|
74
|
1 |
|
substr($curlReturn, 0, $headerSize), |
|
75
|
1 |
|
substr($curlReturn, $headerSize) |
|
76
|
1 |
|
); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @param string $headerString |
|
81
|
|
|
* @param string $body |
|
82
|
|
|
* |
|
83
|
|
|
* @return \Debril\RssAtomBundle\Driver\HttpDriverResponse |
|
84
|
|
|
*/ |
|
85
|
1 |
|
public function getHttpResponse($headerString, $body) |
|
86
|
|
|
{ |
|
87
|
1 |
|
$headers = array(); |
|
88
|
1 |
|
preg_match('/(?<version>\S+) (?P<code>\d+) (?P<message>\V+)/', $headerString, $headers); |
|
89
|
|
|
|
|
90
|
1 |
|
$response = new HttpDriverResponse(); |
|
91
|
|
|
|
|
92
|
1 |
|
$response->setBody($body); |
|
93
|
1 |
|
$response->setHttpCode($headers['code']); |
|
94
|
1 |
|
$response->setHttpMessage($headers['message']); |
|
95
|
1 |
|
$response->setHttpVersion($headers['version']); |
|
96
|
1 |
|
$response->setHeaders($headerString); |
|
97
|
|
|
|
|
98
|
1 |
|
return $response; |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|