1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Njasm\Soundcloud\Request; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* SoundCloud API wrapper in PHP |
7
|
|
|
* |
8
|
|
|
* @author Nelson J Morais <[email protected]> |
9
|
|
|
* @copyright 2014 Nelson J Morais <[email protected]> |
10
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php MIT |
11
|
|
|
* @link http://github.com/njasm/soundcloud |
12
|
|
|
* @package Njasm\Soundcloud |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
class Response implements ResponseInterface |
16
|
|
|
{ |
17
|
|
|
private $httpVersion; |
18
|
|
|
private $httpCode; |
19
|
|
|
private $httpCodeString; |
20
|
|
|
private $response; |
21
|
|
|
private $info; |
22
|
|
|
private $errno; |
23
|
|
|
private $errorString; |
24
|
|
|
private $headers = array(); |
25
|
|
|
private $body; |
26
|
|
|
|
27
|
19 |
|
public function __construct($response, array $info, $errno, $errorString) |
28
|
|
|
{ |
29
|
19 |
|
$this->response = $response; |
30
|
19 |
|
$this->info = $info; |
31
|
19 |
|
$this->errno = $errno; |
32
|
19 |
|
$this->errorString = $errorString; |
33
|
|
|
|
34
|
19 |
|
list($headers, $body) = $this->buildHeaderParts($response); |
35
|
|
|
|
36
|
19 |
|
$this->buildHeadersArray($headers); |
37
|
19 |
|
$this->body = $body; |
38
|
19 |
|
} |
39
|
|
|
|
40
|
19 |
|
private function buildHeaderParts($response) |
41
|
|
|
{ |
42
|
19 |
|
$parts = explode("\r\n\r\nHTTP/", $response); |
43
|
19 |
|
$parts = (count($parts) > 1 ? 'HTTP/' : '') . array_pop($parts); |
44
|
|
|
|
45
|
19 |
|
return explode("\r\n\r\n", $parts, 2); |
46
|
|
|
} |
47
|
|
|
|
48
|
19 |
|
private function buildHeadersArray($headers) |
49
|
|
|
{ |
50
|
19 |
|
$headers = explode("\n", $headers); |
51
|
19 |
|
$httHead = array_shift($headers); |
52
|
19 |
|
list($this->httpVersion, $this->httpCode, $this->httpCodeString) = explode(" ", $httHead, 3); |
53
|
|
|
|
54
|
19 |
|
$this->httpCode = intval($this->httpCode); |
55
|
|
|
|
56
|
19 |
|
foreach ($headers as $header) { |
57
|
19 |
|
list($key, $value) = explode(": ", $header, 2); |
58
|
19 |
|
$this->headers[trim($key)] = trim($value); |
59
|
|
|
} |
60
|
19 |
|
} |
61
|
|
|
|
62
|
1 |
|
public function getHeaders() |
63
|
|
|
{ |
64
|
1 |
|
return $this->headers; |
65
|
|
|
} |
66
|
|
|
|
67
|
9 |
|
public function hasHeader($header) |
68
|
|
|
{ |
69
|
9 |
|
return array_key_exists($header, $this->headers); |
70
|
|
|
} |
71
|
|
|
|
72
|
8 |
|
public function getHeader($header) |
73
|
|
|
{ |
74
|
8 |
|
return $this->hasHeader($header) ? $this->headers[$header] : null; |
|
|
|
|
75
|
|
|
} |
76
|
|
|
|
77
|
9 |
|
public function bodyRaw() |
78
|
|
|
{ |
79
|
9 |
|
return $this->body; |
80
|
|
|
} |
81
|
|
|
|
82
|
5 |
|
public function bodyObject() |
83
|
|
|
{ |
84
|
5 |
|
$contentType = $this->getHeader('Content-Type'); |
85
|
5 |
|
if (stripos($contentType, 'application/json') !== false) { |
86
|
4 |
|
return json_decode($this->body); |
87
|
|
|
} |
88
|
|
|
|
89
|
1 |
|
throw new \OutOfBoundsException("Last Request Content-Type isn't application/json."); |
90
|
|
|
} |
91
|
|
|
|
92
|
2 |
|
public function bodyArray() |
93
|
|
|
{ |
94
|
2 |
|
$body = $this->bodyRaw(); |
95
|
|
|
|
96
|
2 |
|
return json_decode($body, true); |
97
|
|
|
} |
98
|
|
|
|
99
|
1 |
|
public function getHttpVersion() |
100
|
|
|
{ |
101
|
1 |
|
return $this->httpVersion; |
102
|
|
|
} |
103
|
|
|
|
104
|
1 |
|
public function getHttpCode() |
105
|
|
|
{ |
106
|
1 |
|
return $this->httpCode; |
107
|
|
|
} |
108
|
|
|
|
109
|
1 |
|
public function getInfo() |
110
|
|
|
{ |
111
|
1 |
|
return $this->info; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function getErrorNo() |
115
|
|
|
{ |
116
|
|
|
return $this->errno; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public function getErrorString() |
120
|
|
|
{ |
121
|
|
|
return $this->errorString; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
public function getRaw() |
125
|
|
|
{ |
126
|
|
|
return $this->response; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
public function getHttpCodeString() |
130
|
|
|
{ |
131
|
|
|
return $this->httpCodeString; |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: