所以我正在写一个漂亮的小网络应用程序,让我可以从任何地方控制我的各种照明系统,它会很棒。只是在飞利浦Hue API上遇到了一些麻烦。
我可以使PUT请求正常,并且我收到了大量数据,但是在运行以下代码时出现的错误让我感到有点困惑:
$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);
请注意this answer
中的文件数据解决方案错误如下:
[{"error":
{
"type":7,
"address":"/lights/3/state/on",
"description":"invalid value, false }, for parameter, on"
}}]
我知道这是$body
内容的语法错误,但我不能确定错误的正确或反正。我尝试将true
作为字符串发送并尝试将1
作为整数和字符串发送但是它们都返回了相同的错误(显然替换了false
以查找发送的内容)
可以欣赏一些帮助:) 干杯
答案 0 :(得分:0)
糟糕,我弄清楚问题是什么!
出于某种原因,json_encode
在true
,false
和数字周围加上了引号。修复了一点preg_replace
并且它有效! :d
$body = preg_replace("/:\"([0-9]+|true|false)\"/", ":$1",
json_encode(array("on" => true))
);