Trying to use PHP cURL to call published functions - SOLVED

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 = 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.)


  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

        case "DisableMD":
            $menu_selection = "Disable_Motion_Detection No Action";

        case "RqstStats":
            $menu_selection = "Resuest_Stats No Action";

        case "LightOn":
            $menu_selection = "Turn_ON_Freez_Light";
            $url = -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 );

        case "LightOff":
            $menu_selection = "Turn_OFF_Freez_Light No Action";

            $menu_selection = "Oops, Nothing Selected!";

  else if (isset($_POST['Cancel'])) { 
    $Cancelled = "Yes";
    $ClrFlds = 1;
  else {
    $menu_selection = "[]";
    $tst_msg = "[]";
    $response = "[]";

I think you’re missing a double quote before https. The URL string won’t be valid without it.

$url = https://

Wow! Sharp eye and fast response. Actually, the missing quote was only in my writing the topic. The quote is my my PHP code. I obviously don’t know much about formatting these topics. Got some strange stuff.

Maybe @wgbartley could lend a sharp eye here.

For formatting code, use three back-accent marks on a single line at the start and end of the code block. I will edit your code above for you.

 code here
1 Like

At the risk of expressing opinion: I’ve used several forums over the years. The Particle Community is the most helpful I’ve seen!
Thanks guys for helping a Particle noobie.


I’m shooting from the hip here (not testing my code), but I think this should cover the portion you’re having troubles with. You’ll want to transmit the data and access token using CURLOPT_POSTFIELDS and CURLOPT_HTTPHEADER respectively. There are a ton of curl_setopt options, so it’s definitely easy to get lost.

$url = "";

$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);
curl_setopt( $ch, CURLOPT_POSTFIELDS, array('arg' => 'on'));
curl_setopt( $ch, CURLOPT_HTTPHEADER, array('Authorization: Bearer my_access_token');

You may also want to check out @harrisonhjones’s phpParticle library. It takes care of all those cURL options behind the scenes.


Mr. Bartley, you solved my issue!


is this still working? I have used this code successfully up until a couple of days ago, when it suddenly stopped working. I did not change anything and now I am searching for a solution. Did the photon.function() call change? Or something in the PHP curl library?

I am thankful for every hint.


Take a look at [FIXED] Php curl - new problem with Particle.function. Apparently you need to encode your POST fields now

1 Like

thank you!