forked from bmoesdijk/daikin-control-UI-updated
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdaikin_client.php
More file actions
50 lines (43 loc) · 1.7 KB
/
Copy pathdaikin_client.php
File metadata and controls
50 lines (43 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<?php
class DaikinClientException extends RuntimeException {}
function daikin_get($ip, $uri, $timeout_seconds = 5) {
return daikin_request($ip, $uri, null, $timeout_seconds);
}
function daikin_post($ip, $uri, array $params, $timeout_seconds = 5) {
return daikin_request($ip, $uri, $params, $timeout_seconds);
}
function daikin_request($ip, $uri, $params, $timeout_seconds) {
$url = sprintf('http://%s%s', $ip, $uri);
if ($params !== null && $params !== []) {
$url .= '?' . http_build_query($params);
}
$ctx = stream_context_create([
'http' => [
'method' => 'GET',
'timeout' => $timeout_seconds, // read timeout
'ignore_errors' => true, // give us the body even on 4xx/5xx
],
]);
$prev_socket_timeout = ini_get('default_socket_timeout');
ini_set('default_socket_timeout', (string) $timeout_seconds);
error_clear_last();
$body = @file_get_contents($url, false, $ctx);
ini_set('default_socket_timeout', $prev_socket_timeout);
if ($body === false) {
$err = error_get_last();
$detail = ($err && isset($err['message'])) ? ': ' . $err['message'] : '';
throw new DaikinClientException("network error talking to $ip$detail");
}
$code = 0;
if (isset($http_response_header) && is_array($http_response_header)) {
foreach ($http_response_header as $h) {
if (preg_match('#^HTTP/\S+\s+(\d+)#', $h, $m)) {
$code = (int) $m[1];
}
}
}
if ($code !== 0 && ($code < 200 || $code >= 300)) {
throw new DaikinClientException("device $ip returned HTTP $code");
}
return $body;
}