PHP HTTP CURL PUT请求LIFX电源开/关 [英] PHP HTTP CURL PUT request for LIFX Power On/Off
问题描述
我尝试使用PHP启动/关闭所有Lifx灯泡。
I'm trying to power all of my Lifx bulbs on/off using PHP.
API文档 http://developer.lifx.com/ ,表示要使用PUT请求:
The API documentation, http://developer.lifx.com/, says to use a PUT request:
curl -u "c87c73a896b554367fac61f71dd3656af8d93a525a4e87df5952c6078a89d192:" \
-X PUT \
-d "state=on" \
"https://api.lifx.com/v1beta1/lights/all/power"
现在使用curl命令在命令行中工作。它提示我输入我的密码,除非我在用户名的冒号之后添加。
Now using that curl command works in the command line. It prompts me for my password unless I add it after the colon in the "username".
麻烦的是我试图将这个命令翻译成PHP,如下:
The trouble is when I try to translate that command into PHP like so:
$authToken = 'c87c73a896b554367fac61f71dd3656af8d93a525a4e87df5952c6078a89d192:myFakePassword';
$ch = curl_init('https://api.lifx.com/v1beta1/lights/all/power');
$headers = array("selector=all&state=on",'Authorization: Bearer ' . $authToken);
curl_setopt($ch, CURLOPT_PUT, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
这经历了,但我得到一个404未找到,Lifx文档说可能是一个格式错误的选择器
This goes through, but I get a 404 Not Found which the Lifx documentation says is probably a malformed selector.
注意:我可以使用PHP成功调用这个POST来切换电源:
Note: I was able to make a successful call with PHP to toggle the power with this POST:
$authToken = 'c87c73a896b554367fac61f71dd3656af8d93a525a4e87df5952c6078a89d192';
$ch = curl_init('https://api.lifx.com/v1beta1/lights/all/toggle');
$headers = array("selector=all",'Authorization: Bearer ' . $authToken);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($ch);
但我不想只是切换灯,我想要能够指定on关闭。
But I don't want to just toggle the lights, I want to be able to specify on or off. What could be wrong with my PUT request?
推荐答案
通过设置一些其他curl选项来解决这个问题:
Solved this by setting some other curl options:
$ch = curl_init($link);
$headers = array('Authorization: Bearer ' . $authToken);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$data = "state=on";
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$response = curl_exec($ch);
$ link是https://api.lifx.com/v1beta1/lights/all/power
$link is https://api.lifx.com/v1beta1/lights/all/power
$ authToken是我的api键
$authToken is my api key
这篇关于PHP HTTP CURL PUT请求LIFX电源开/关的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!