I was answering a question for someone who was using our HTTP API with PHP the other day and ended up writing some sample code. I thought it might be useful to people here.
If you place your authentication token in $authToken
and then run the script it will attempt to turn on all the lights on your LIFX account. If it doesn’t work it outputs the error message from curl, and if it does work it returns the data structure from our API.
<?php
$link = "https://api.lifx.com/v1beta1/lights/all/power";
$authToken = "";
$ch = curl_init($link);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$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);
?>
<html>
<head>
<title>LIFX API Test</title>
</head>
<body>
<h1>LIFX API Test</h1>
<pre><?php
if(curl_errno($ch))
{
echo 'Curl error: ' . curl_error($ch);
} else {
echo var_dump(json_decode($response, true));
}
?></pre>
</body>
</html>
<?php
curl_close($ch);
?>