1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Openbuildings\Spiderling; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Load urls using Curl |
7
|
|
|
* |
8
|
|
|
* @package Openbuildings\Spiderling |
9
|
|
|
* @author Ivan Kerin |
10
|
|
|
* @copyright (c) 2013 OpenBuildings Ltd. |
11
|
|
|
* @license http://spdx.org/licenses/BSD-3-Clause |
12
|
|
|
*/ |
13
|
|
|
class Driver_Simple_RequestFactory_HTTP implements Driver_Simple_RequestFactory |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* The user agent to be used when performing the requests |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
protected $_user_agent = 'Spiderling Simple Driver'; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* The last visited url address |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
protected $_current_url; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Getter / Setter for the user agent, used when performing the requests |
29
|
|
|
* @param string $user_agent |
30
|
|
|
* @return string|Driver_Simple_RequestFactory_HTTP |
31
|
|
|
*/ |
32
|
|
|
public function user_agent($user_agent = NULL) |
33
|
|
|
{ |
34
|
|
|
if ($user_agent !== NULL) |
35
|
|
|
{ |
36
|
|
|
$this->_user_agent = $user_agent; |
37
|
|
|
return $this; |
38
|
|
|
} |
39
|
|
|
return $this->_user_agent; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Get the url of the last request |
44
|
|
|
* @return string |
45
|
|
|
*/ |
46
|
|
|
public function current_url() |
47
|
|
|
{ |
48
|
|
|
return $this->_current_url; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Get the path (no protocol or host) of the last request |
53
|
|
|
* @return [type] [description] |
|
|
|
|
54
|
|
|
*/ |
55
|
|
|
public function current_path() |
56
|
|
|
{ |
57
|
|
|
$url = parse_url($this->current_url()); |
58
|
|
|
|
59
|
|
|
return $url['path'].(isset($url['query']) ? '?'.$url['query'] : ''); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Perform the request, follow redirects, return the response |
64
|
|
|
* @param string $method |
65
|
|
|
* @param string $url |
66
|
|
|
* @param array $post |
67
|
|
|
* @return string |
68
|
|
|
*/ |
69
|
|
|
public function execute($method, $url, array $post = array()) |
70
|
|
|
{ |
71
|
|
|
$curl = curl_init($url); |
72
|
|
|
|
73
|
|
|
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, TRUE); |
74
|
|
|
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE); |
75
|
|
|
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method); |
76
|
|
|
curl_setopt($curl, CURLOPT_USERAGENT, $this->user_agent()); |
77
|
|
|
|
78
|
|
|
if ($post) |
|
|
|
|
79
|
|
|
{ |
80
|
|
|
curl_setopt($curl, CURLOPT_POSTFIELDS, $post); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$response = curl_exec($curl); |
84
|
|
|
|
85
|
|
|
if ($response === FALSE OR curl_getinfo($curl, CURLINFO_HTTP_CODE) !== 200) |
86
|
|
|
{ |
87
|
|
|
throw new Exception_Curl('Curl: Download Error: :error, status :status on url :url', array(':url' => $url, ':status' => curl_getinfo($curl, CURLINFO_HTTP_CODE), ':error' => curl_error($curl))); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$this->_current_url = urldecode(curl_getinfo($curl, CURLINFO_EFFECTIVE_URL)); |
91
|
|
|
|
92
|
|
|
return $response; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.