|
1
|
|
|
<?php |
|
2
|
|
|
namespace Redbox\Twitch\Transport\Adapter; |
|
3
|
|
|
|
|
4
|
|
|
class Curl implements AdapterInterface |
|
5
|
|
|
{ |
|
6
|
|
|
/** |
|
7
|
|
|
* @var resource |
|
8
|
|
|
*/ |
|
9
|
|
|
protected $curl; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* @var int |
|
13
|
|
|
*/ |
|
14
|
|
|
protected $timeout = 30; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @var int |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $connect_timeout = 30; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Verify that we can support the curl extention. |
|
23
|
|
|
* If this is not the case we will throw a BadFunctionCallException. |
|
24
|
|
|
* |
|
25
|
|
|
* @throws BadFunctionCallException |
|
26
|
|
|
* @return bool |
|
27
|
|
|
*/ |
|
28
|
|
|
public function verifySupport() |
|
29
|
|
|
{ |
|
30
|
|
|
if (!extension_loaded('curl')) { |
|
31
|
|
|
throw new \BadFunctionCallException('The cURL extension is required.'); |
|
32
|
|
|
} |
|
33
|
|
|
return true; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Initialize curl and close the connection if |
|
38
|
|
|
* needed. |
|
39
|
|
|
*/ |
|
40
|
|
|
public function open() |
|
41
|
|
|
{ |
|
42
|
|
|
if (is_resource($this->curl)) { |
|
43
|
|
|
$this->close(); |
|
44
|
|
|
} |
|
45
|
|
|
$this->curl = curl_init(); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Close the curl connection |
|
50
|
|
|
*/ |
|
51
|
|
|
public function close() |
|
52
|
|
|
{ |
|
53
|
|
|
if ($this->curl) { |
|
54
|
|
|
curl_close($this->curl); |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Send the request to the server. |
|
61
|
|
|
* |
|
62
|
|
|
* @param $address |
|
63
|
|
|
* @param $method |
|
64
|
|
|
* @param null $headers |
|
65
|
|
|
* @param null $body |
|
66
|
|
|
* @return mixed |
|
67
|
|
|
*/ |
|
68
|
|
|
public function send($address, $method, $headers = null, $body = null) |
|
69
|
|
|
{ |
|
70
|
|
|
// Set connection options |
|
71
|
|
|
curl_setopt($this->curl, CURLOPT_URL, $address); |
|
72
|
|
|
curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, $method); |
|
73
|
|
|
curl_setopt($this->curl, CURLOPT_HEADER, false); |
|
74
|
|
|
curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true); |
|
75
|
|
|
curl_setopt($this->curl, CURLOPT_VERBOSE, false); |
|
76
|
|
|
|
|
77
|
|
|
if (is_array($headers)) { |
|
78
|
|
|
$curlHeaders = array(); |
|
79
|
|
|
foreach ($headers as $k => $v) { |
|
80
|
|
|
$curlHeaders[] = "$k: $v"; |
|
81
|
|
|
} |
|
82
|
|
|
curl_setopt($this->curl, CURLOPT_HTTPHEADER, $curlHeaders); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
if (is_array($body) === true && count($body) > 0) { |
|
86
|
|
|
curl_setopt($this->curl, CURLOPT_POSTFIELDS, $body); |
|
87
|
|
|
} |
|
88
|
|
|
return curl_exec($this->curl); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Return the HTTP status code |
|
93
|
|
|
* |
|
94
|
|
|
* @return mixed |
|
95
|
|
|
*/ |
|
96
|
|
|
public function getHttpStatusCode() |
|
97
|
|
|
{ |
|
98
|
|
|
return curl_getinfo($this->curl, CURLINFO_HTTP_CODE); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Return the content-type header set by the server. |
|
103
|
|
|
* |
|
104
|
|
|
* @return mixed |
|
105
|
|
|
*/ |
|
106
|
|
|
public function getContentType() |
|
107
|
|
|
{ |
|
108
|
|
|
return curl_getinfo($this->curl, CURLINFO_CONTENT_TYPE); |
|
109
|
|
|
} |
|
110
|
|
|
} |