1

So I'm writing a nice little web app that lets me control my various lighting systems from anywhere and it's going great. Just hit a bit of a snag with the Philips Hue API.

I can make a PUT request fine, and I am getting plenty of data back, but am a bit confused by the error that I get when running the following code:

$ch = curl_init();

$body = json_encode(array("on" => true));
// Translates to String '{"on":true}'

$fp = fopen('php://temp/maxmemory:256000', 'w');
fwrite($fp, $body);
fseek($fp, 0);

curl_setopt($ch, CURLOPT_URL, 'https://client-eastwood-dot-hue-prod-us.appspot.com/api/0/lights/1');
curl_setopt($ch, CURLOPT_PUT, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
curl_setopt($ch, CURLOPT_INFILE, $fp);
curl_setopt($ch, CURLOPT_INFILESIZE, strlen($body));
curl_setopt($ch, CURLOPT_HTTPHEADER, array
(
    "Content-Type: application/json;charset=UTF-8",
    "X-Token: $Token"
));

curl_exec($ch);
curl_close($ch);

Note the file data solution from this answer

The error comes out as follows:

[{"error":
{
    "type":7,
    "address":"/lights/3/state/on",
    "description":"invalid value,  false }, for parameter, on"
}}]

I know it's a syntax error with the $body content but I can't make heads or tails of what, exactly, is wrong. I've tried sending true through as a string and tried sending 1 as an integer and string but it all comes back with the same error (replacing false for whatever is sent, obviously).

Could appreciate some help :)
Cheers

tk421
  • 5,775
  • 6
  • 23
  • 34

1 Answers1

0

Whoops, I figured out what the problem was!

For some reason json_encode was putting quotation marks around true, false and numbers. Fixed that with a little preg_replace and it works! :D

$body = preg_replace("/:\"([0-9]+|true|false)\"/", ":$1",
    json_encode(array("on" => true))
);