Toggle lights PHP problem

Trying to get some basic PHP code running, but have hit a curious problem with the toggle lights function. It works if I use the curl command directly e.g.

curl -X POST “https://api.lifx.com/v1/lights/group:Study/toggle” -H “Authorization: Bearer c…9” -d “duration=1.0”

or the example on the docs page.

My initial PHP code was (based on the example page and other sports here):

<?php $link = "https://api.lifx.com/v1/lights/group:Study/toggle"; $authToken = "c...........9"; $ch = curl_init($link); $headers = array('Authorization: Bearer ' . $authToken); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_POST, true); $response = curl_exec($ch); ?>

But this gives me a ‘400 Bad Request’ error

If I include duration data,e.g.

<?php $link = "https://api.lifx.com/v1/lights/group:Study/toggle"; $authToken = "c..........9"; $ch = curl_init($link); $headers = array('Authorization: Bearer ' . $authToken); $data = 'duration=1.0' curl_setopt($ch, CURLOPT_POSTFIELDS, $data); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_POST, true); $response = curl_exec($ch); ?>

there is no error reported, but no response from the light(s) or in the browser. I’ve tried various :selector values (even ‘all’) and the behaviour is consistent.

Feel I must be missing something obvious, but it’s eluded me so far…

Both of these examples worked for me. The second one is missing the end of line semicolon after the duration assignment line though, which is an easy fix. When I put in the wrong token or selector I get output that correctly highlights the issue.

When using PHP there are a few things you should check:

  1. Check that your SSL CA certificates are up to date.
  2. Try turning debugging on directly after you run curl_init using curl_setopt($ch, CURLOPT_VERBOSE, 1);
  3. Look in your error logs on the server, your php.ini might be configured to only deliver errors there.

Let me know if that helps you get any further…

Thanks Daniel - adding the missing ‘;’ got the second example working :slight_smile:. Still can’t get the first to behave though - not a big problem if the second behaves.

I also noticed a slight error on the ‘Activate scene’ example page.

In the PHP example code it has:

$link = “https://api.lifx.com/v1/scenes/scene_uuid:$scene/activate”;

but the required link should be:

https://api.lifx.com/v1/scenes/scene_id::scene_uuid/activate.

At least I’m comfortable that the basic foundations are set up now.

Thanks! I’ve made those corrections now.