|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @category Brownie/HttpClient |
|
4
|
|
|
* @author Brownie <[email protected]> |
|
5
|
|
|
* @license https://opensource.org/licenses/MIT |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace Brownie\HttpClient\Client; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* API CURL functions. |
|
12
|
|
|
*/ |
|
13
|
|
|
class CurlAdaptee |
|
14
|
|
|
{ |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Initialize a cURL session. |
|
18
|
|
|
* Returns a cURL handle on success, FALSE on errors. |
|
19
|
|
|
* |
|
20
|
|
|
* @param string $url URL. |
|
21
|
|
|
* |
|
22
|
|
|
* @return resource |
|
23
|
|
|
*/ |
|
24
|
7 |
|
public function init($url) |
|
25
|
|
|
{ |
|
26
|
7 |
|
return curl_init($url); |
|
|
|
|
|
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Set an option for a cURL transfer. |
|
31
|
|
|
* Returns the current object. |
|
32
|
|
|
* |
|
33
|
|
|
* @param resource $curl A cURL handle. |
|
34
|
|
|
* @param int $option The CURLOPT_XXX option to set. |
|
35
|
|
|
* @param mixed $value The value to be set on option. |
|
36
|
|
|
* |
|
37
|
|
|
* @return self |
|
38
|
|
|
*/ |
|
39
|
1 |
|
public function setopt($curl, $option, $value) |
|
40
|
|
|
{ |
|
41
|
1 |
|
curl_setopt($curl, $option, $value); |
|
42
|
1 |
|
return $this; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Perform a cURL session. |
|
47
|
|
|
* Returns TRUE on success or FALSE on failure. However, |
|
48
|
|
|
* if the CURLOPT_RETURNTRANSFER option is set, it will |
|
49
|
|
|
* return the result on success, FALSE on failure. |
|
50
|
|
|
* |
|
51
|
|
|
* @param resource $curl A cURL handle. |
|
52
|
|
|
* |
|
53
|
|
|
* @return mixed |
|
54
|
|
|
*/ |
|
55
|
1 |
|
public function exec($curl) |
|
56
|
|
|
{ |
|
57
|
1 |
|
return curl_exec($curl); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Get information regarding a specific transfer. |
|
62
|
|
|
* If opt is given, returns its value. Otherwise, returns an associative array |
|
63
|
|
|
* with the following elements (which correspond to opt), or FALSE on failure. |
|
64
|
|
|
* |
|
65
|
|
|
* @param resource $curl A cURL handle. |
|
66
|
|
|
* @param int $opt CURLINFO constant. |
|
67
|
|
|
* |
|
68
|
|
|
* @return mixed |
|
69
|
|
|
*/ |
|
70
|
1 |
|
public function getinfo($curl, $opt) |
|
71
|
|
|
{ |
|
72
|
1 |
|
return curl_getinfo($curl, $opt); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Return the last error number. |
|
77
|
|
|
* |
|
78
|
|
|
* @param resource $curl A cURL handle. |
|
79
|
|
|
* |
|
80
|
|
|
* @return int |
|
81
|
|
|
*/ |
|
82
|
1 |
|
public function errno($curl) |
|
83
|
|
|
{ |
|
84
|
1 |
|
return curl_errno($curl); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Return a string containing the last error for the current session. |
|
89
|
|
|
* |
|
90
|
|
|
* @param resource $curl A cURL handle. |
|
91
|
|
|
* |
|
92
|
|
|
* @return string |
|
93
|
|
|
*/ |
|
94
|
1 |
|
public function error($curl) |
|
95
|
|
|
{ |
|
96
|
1 |
|
return curl_error($curl); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Close a cURL session. |
|
101
|
|
|
* Returns the current object. |
|
102
|
|
|
* |
|
103
|
|
|
* @param resource $curl A cURL handle. |
|
104
|
|
|
* |
|
105
|
|
|
* @return self |
|
106
|
|
|
*/ |
|
107
|
1 |
|
public function close($curl) |
|
108
|
|
|
{ |
|
109
|
1 |
|
curl_close($curl); |
|
110
|
1 |
|
return $this; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Returns user agent string. |
|
115
|
|
|
* |
|
116
|
|
|
* @return string |
|
117
|
|
|
*/ |
|
118
|
1 |
|
public function getAgentString() |
|
119
|
|
|
{ |
|
120
|
1 |
|
$info = curl_version(); |
|
121
|
1 |
|
return 'PHP Curl ' . $info['version'] . ', SSL: ' . $info['ssl_version']; |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|
If the returned type also contains false, it is an indicator that maybe an error condition leading to the specific return statement remains unhandled.