I need help with a web interface to Particle PHOTON functions. I started with the Code Example “Control LEDs over the net”.
That works fine for what it is. But, I wanted to accomplish 3 goals in addition to what the Code Example offered. They are:
1) I need to protect my device id’s and access token. The Code example exposes the device id and access token to anyone that uses their browser to view the source code;
2) I wanted to “deal with“ Particle.api’s response;
3) I need to select different functions from different devices depending on selections made web page’s menu.
So, I altered the html code used in the Code Example. Basically, I’m attempting to use PHP’s cURL functionality to make the call to Particle.api rather than the form’s action field. But, I’m not having much success.
I’m running into problems creating the string cURL needs for the curl_init(string”) or I’m not using curl_setopt() correctly. I’ve looked at docs.particle.reference/api/#call-a-function and how they use CLI curl and used that example as a basis for my PHP cURL strings.
Listed below are some of the results I get when I go to my web page and select the radio button “Turn Light On”:
$menus_selection = "Turn_ON_Freeze_Light"
as I expect, so I know I’m in the correct section of PHP code;
$url = https://api.particle.io/v1/devices/my_device_id/led-d arg='on' -d access_token=my_access_token
as I expected;
$response = { "error": "invalid_request", "error_description": "The access token was not found" }
And there’s my problem.
Any help would be greatly appreciated!
Here is the PHP script I’m calling from an HTML form:
(It’s only the 4th case of switch that’s really of interest.)
<?PHP
if (isset($_POST['Submit'])) {
$menu_selection = $_POST['menu_sel'];
switch ($_POST['menu_sel']) {
case "EnableMD":
$menu_selection = "Enable_Motion_Detection No Action"; # used for debugging
break;
case "DisableMD":
$menu_selection = "Disable_Motion_Detection No Action";
break;
case "RqstStats":
$menu_selection = "Resuest_Stats No Action";
break;
case "LightOn":
$menu_selection = "Turn_ON_Freez_Light";
$url = https://api.particle.io/v1/devices/my_device_id/led -d arg=’on’ -d access_token=my_access_token";
$ch = curl_init( $url );
curl_setopt( $ch, CURLOPT_POST, 1);
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt( $ch, CURLOPT_HEADER, 0);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec( $ch );
break;
case "LightOff":
$menu_selection = "Turn_OFF_Freez_Light No Action";
break;
default:
$menu_selection = "Oops, Nothing Selected!";
}
}
else if (isset($_POST['Cancel'])) {
$Cancelled = "Yes";
$ClrFlds = 1;
}
else {
$menu_selection = "[]";
$tst_msg = "[]";
$response = "[]";
}
?>