|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Onoi\HttpRequest; |
|
4
|
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* @license GNU GPL v2+ |
|
9
|
|
|
* @since 1.0 |
|
10
|
|
|
* |
|
11
|
|
|
* @author mwjames |
|
12
|
|
|
*/ |
|
13
|
|
|
class CurlRequest implements HttpRequest { |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @var resource |
|
17
|
|
|
*/ |
|
18
|
|
|
private $handle; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var array |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $options = array(); |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @since 1.0 |
|
27
|
|
|
* |
|
28
|
|
|
* @param resource $handle |
|
29
|
|
|
*/ |
|
30
|
11 |
|
public function __construct( $handle ) { |
|
31
|
|
|
|
|
32
|
11 |
|
if ( get_resource_type( $handle ) !== 'curl' ) { |
|
33
|
1 |
|
throw new InvalidArgumentException( "Expected a cURL resource type" ); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
10 |
|
$this->handle = $handle; |
|
37
|
10 |
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @since 1.0 |
|
41
|
|
|
* |
|
42
|
|
|
* @return boolean |
|
43
|
|
|
*/ |
|
44
|
2 |
|
public function ping() { |
|
45
|
|
|
|
|
46
|
2 |
|
if ( curl_getinfo( $this->handle, CURLINFO_EFFECTIVE_URL ) === '' ) { |
|
47
|
1 |
|
return false; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
// Copy the handle to avoid diluting the resource |
|
51
|
2 |
|
$handle = curl_copy_handle( $this->handle ); |
|
52
|
|
|
|
|
53
|
2 |
|
curl_setopt_array( $handle, array( |
|
54
|
2 |
|
CURLOPT_HEADER => false, |
|
55
|
2 |
|
CURLOPT_RETURNTRANSFER, true, |
|
56
|
2 |
|
CURLOPT_CONNECTTIMEOUT => 5, |
|
57
|
2 |
|
CURLOPT_FRESH_CONNECT => false, |
|
58
|
2 |
|
CURLOPT_FAILONERROR => true |
|
59
|
2 |
|
) ); |
|
60
|
|
|
|
|
61
|
2 |
|
curl_exec( $handle ); |
|
62
|
|
|
|
|
63
|
2 |
|
return curl_errno( $handle ) == 0; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @since 1.0 |
|
68
|
|
|
* |
|
69
|
|
|
* @param string $name |
|
70
|
|
|
* @param mixed $value |
|
71
|
|
|
*/ |
|
72
|
7 |
|
public function setOption( $name, $value ) { |
|
73
|
|
|
|
|
74
|
7 |
|
$this->options[$name] = $value; |
|
75
|
|
|
|
|
76
|
|
|
// Internal ONOI options are not further relayed |
|
77
|
7 |
|
if ( strpos( $name, 'ONOI_HTTP_REQUEST' ) !== false ) { |
|
78
|
3 |
|
return; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
6 |
|
curl_setopt( |
|
82
|
6 |
|
$this->handle, |
|
83
|
6 |
|
$name, |
|
84
|
|
|
$value |
|
85
|
6 |
|
); |
|
86
|
6 |
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @since 1.0 |
|
90
|
|
|
* |
|
91
|
|
|
* @param string $name |
|
92
|
|
|
* |
|
93
|
|
|
* @return mixed|null |
|
94
|
|
|
*/ |
|
95
|
3 |
|
public function getOption( $name ) { |
|
96
|
3 |
|
return isset( $this->options[$name] ) ? $this->options[$name] : null; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @since 1.0 |
|
101
|
|
|
* |
|
102
|
|
|
* @param string|null $name |
|
103
|
|
|
* |
|
104
|
|
|
* @return mixed |
|
105
|
|
|
*/ |
|
106
|
2 |
|
public function getLastTransferInfo( $name = null ) { |
|
107
|
2 |
|
return curl_getinfo( $this->handle, $name ); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* @since 1.0 |
|
112
|
|
|
* |
|
113
|
|
|
* @return string |
|
114
|
|
|
*/ |
|
115
|
1 |
|
public function getLastError() { |
|
116
|
1 |
|
return curl_error( $this->handle ); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* @since 1.0 |
|
121
|
|
|
* |
|
122
|
|
|
* @return integer |
|
123
|
|
|
*/ |
|
124
|
2 |
|
public function getLastErrorCode() { |
|
125
|
2 |
|
return curl_errno( $this->handle ); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* @since 1.0 |
|
130
|
|
|
* |
|
131
|
|
|
* @return mixed |
|
132
|
|
|
*/ |
|
133
|
2 |
|
public function execute() { |
|
134
|
2 |
|
$this->options = array(); |
|
135
|
2 |
|
return curl_exec( $this->handle ); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* Use __invoke to return the handle in order to avoid cluttering the |
|
140
|
|
|
* interface |
|
141
|
|
|
* |
|
142
|
|
|
* @since 1.0 |
|
143
|
|
|
*/ |
|
144
|
3 |
|
public function __invoke() { |
|
145
|
3 |
|
return $this->handle; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* @since 1.0 |
|
150
|
|
|
*/ |
|
151
|
10 |
|
public function __destruct() { |
|
152
|
10 |
|
curl_close( $this->handle ); |
|
153
|
10 |
|
} |
|
154
|
|
|
|
|
155
|
|
|
} |
|
156
|
|
|
|