1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NodeRED; |
4
|
|
|
|
5
|
|
|
use GuzzleHttp\Client; |
6
|
|
|
|
7
|
|
|
class Instance |
8
|
|
|
{ |
9
|
|
|
private $endpoint; |
10
|
|
|
private $client; |
11
|
|
|
|
12
|
3 |
|
public function __construct($endpoint, Client $client = null) |
13
|
|
|
{ |
14
|
3 |
|
if (null === $client) { |
15
|
|
|
$client = new Client(); |
16
|
|
|
} |
17
|
|
|
|
18
|
3 |
|
$this->endpoint = $endpoint; |
19
|
3 |
|
$this->client = $client; |
20
|
3 |
|
} |
21
|
|
|
|
22
|
2 |
|
public function get($path, $token = null) |
23
|
|
|
{ |
24
|
2 |
|
return $this->makeRequest('GET', $path, [], $token); |
25
|
|
|
} |
26
|
|
|
|
27
|
1 |
|
public function formPost($path, $data, $token = null) |
28
|
|
|
{ |
29
|
1 |
|
return $this->makeRequest('POST', $path, ['form_params' => $data], $token); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function jsonPost($path, $data, $token = null) |
33
|
|
|
{ |
34
|
|
|
return $this->makeRequest('POST', $path, ['json' => $data], $token); |
35
|
|
|
} |
36
|
|
|
|
37
|
3 |
|
private function makeRequest($method, $path, $extraOptions = [], $token = null) |
38
|
|
|
{ |
39
|
|
|
$options = [ |
40
|
|
|
'headers' => [ |
41
|
3 |
|
'Node-RED-API-Version' => 'v2', |
42
|
3 |
|
], |
43
|
3 |
|
'http_errors' => false, |
44
|
3 |
|
]; |
45
|
|
|
|
46
|
3 |
|
if (null !== $token) { |
47
|
|
|
$options['headers']['Authorization'] = $token->getAuthorizationString(); |
48
|
|
|
} |
49
|
|
|
|
50
|
3 |
|
$response = $this->client->request($method, $this->endpoint . $path, array_merge($options, $extraOptions)); |
51
|
|
|
|
52
|
3 |
|
return $this->processResponse($response); |
|
|
|
|
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
private function processResponse(ResponseInterface $response) |
56
|
|
|
{ |
57
|
|
|
$statusCode = $response->getStatusCode(); |
58
|
|
|
|
59
|
|
|
if ($statusCode < 200 || $statusCode >= 300) { |
60
|
|
|
throw ApiException::fromResponse($response); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$json = json_decode($response->getBody(), true); |
64
|
|
|
|
65
|
|
|
if (NULL === $json) { |
66
|
|
|
throw new ApiException('Could not get JSON from response', 0, $response); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return $json; |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
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: