簡體   English   中英

飛利浦Hue API CURL PUT請求返回異常錯誤

[英]Philips Hue API CURL PUT request returning bizarre error

因此,我正在編寫一個不錯的小型Web應用程序,它使我可以從任何地方控制各種照明系統,而且一切都很好。 飛利浦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);

注意此答案中的文件數據解決方案

錯誤如下:

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

我知道這是$body內容的語法錯誤,但是我無法弄清楚到底是什么錯誤。 我嘗試通過字符串形式發送true ,並嘗試以整數和字符串形式發送1 ,但所有結果都返回相同的錯誤(顯然,發送的內容全部替換為false )。

希望得到一些幫助:)
干杯

糟糕,我知道問題出在哪里!

由於某種原因, json_encodetruefalse和數字json_encode加上了引號。 修復了一點點preg_replace ,就可以了! :d

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM