cURL PHP HTTP API toggle power

I am fiddling with the HTTP API trying to toggle my lights from a PHP script. Here is my code:

header('Content-Type: application/json');

$lampid = $_POST['lampid']; // our lampid looks like this: d073d5107598
$token = $_POST['token']; // our API token

$link = "https://api.lifx.com/v1/lights/".$lampid."/toggle";
$ch = curl_init($link);

$headers = array('Authorization: Bearer ' . $token);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

curl_exec($ch);

All I am getting back from this is a simple JSON object:

{"error":"Not found"}

I am thinking that this is an issue with my token/lamp id mismatching, but I have run the same setup through the API example on this page: http://api.developer.lifx.com/docs/toggle-power and I can toggle my lights no worries. That page returns the API link which I have checked against my own PHP generated link and it is the same. I know my API token is correct because this method is being called straight after a successful ‘list’ request using the same credentials.

What am I missing?

If $lampid is “d073d5107598”, then you’re generating the URL:

https://api.lifx.com/v1/lights/d073d5107598/toggle

when it should be:

https://api.lifx.com/v1/lights/id:d073d5107598/toggle

(you’re missing the “id:”)

Hmm, I thought I tried that but I’ll give it another go tonight thanks

The other thought I had: are you sending a POST request? I don’t know PHP, but I don’t see any place where you’re setting the request method.

1 Like

Thank you very much ppelleti. I was neglecting to tell it to use POST. Here is my code:

header('Content-Type: application/json');

$lampid = $_POST['lampid']; // our lampid looks like this: d073d5107598
$token = $_POST['token']; // our API token

$link = "https://api.lifx.com/v1/lights/".$lampid."/toggle";
$ch = curl_init($link);

$headers = array('Authorization: Bearer ' . $token);

curl_setopt($ch, CURLOPT_POST, true); // set POST method
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

curl_exec($ch);

Note that the $link is without the id: but thanks for the suggestion!