[SOLVED] Webhook to PHP script

Hi,
I’m a French enthusiast for electronics and particle device & services.
I hope my English is understandable.

I have created a web app which allow me to read some value from a core/photon.

Now I would like to save theses data in a SQL database every minute without the web app.
From what I saw in the particle forum & documentation I should use this flow:

The link between the php script and My SQL Database is working.

But I’m not sure of the methode to get data from JSON.
here is my PHP code for that ;

$Data = explode('_',$_GET['Data']);

$DateAjout = date ('Y-m-d H:i:s');
$Temperature = $Data[0];
$Humidite = $Data[1];
$Luminosite = $Data[2];

The JSON object come from a webhook I successfully created with the partile cli.
I’ve used this tutorial and here is my code for the JSON file :

{
  "eventName": "SaveToDataBase",
  "url": "https://mathieubertecreations.com/LectureCoreData.php",
  "requestType": "POST",
  "DataFromPhoton":
    {
      "temperature": "{{PHOTON_TEMPERATURE}}",
      "humidite": "{{PHOTON_HUMIDITE}}",
      "luminosite": "{{PHOTON_LUMINOSITE}}"
    },
  "mydevices": true
} 

On my core I use this function :

sprintf(publishString, "{ \"PHOTON_TEMPERATURE\": %u,\"PHOTON_HUMIDITE\": %u, \"PHOTON_LUMINOSITE\": %u }", temperature, humidite, light);

Particle.publish("SaveToDataBase", publishString); 

The problem is that the dashboard send me an error as you can see :

For me the publish fucntion is working but my webhook doesn’t.

Do you have an idea why this is not working ?
Is it because of my php code or my webhook code ?

Thanks for your help

I’ve deleted the “s” of the “https” in the JSON URL.
Now there is no error in the dashboard but the data property in the event hook-sent is empty

Change $_GET in your PHP to $_POST[‘DataFomPhoton’] since it’s getting a POST. The delimiter should be ‘,’ if anything I think, but you’ll still get the ID:data in the elements, so you’ll need to convert each down to the 2nd half by exploding (’:’, $Data[n] ).

I just checked and found this. Might be useful.
http://php.net/manual/en/function.json-decode.php
You should be able to put $_POST[‘DataFomPhoton’] in as the json and get back an array with json elements in it.

Thank you I will try this !

I finally succeed in what I wanted to do
I send the value in the query instead of the json object.

photon code :

sprintf(data, "{ \"temperature\": %u,\"humidite\": %u, \"luminosite\": %u }", temperature, humidite, light);
Particle.publish("SaveToDataBase", data);

JSON file :

{
  "eventName": "SaveToDataBase",
  "url": "http://mathieubertecreations.com/php/LectureCoreData.php",
  "requestType": "POST",
  "data":
    {
      "temperature": "{{temperature}}",
      "humidite": "{{humidite}}",
      "luminosite": "{{luminosite}}"
    },
    "query":
    {
      "temperature": "{{temperature}}",
      "humidite": "{{humidite}}",
      "luminosite": "{{luminosite}}"
    },
  "mydevices": true
}

php file :

<?php 

$DateAjout = date ('Y-m-d H:i:s');
$Temperature = $_GET['temperature'];
$Humidite = $_GET['humidite'];
$Luminosite = $_GET['luminosite'];

?>

I used the json-decode() function which they talk about in you link. It was working with my own json string like in the php file but not with the json from the webhook. I used $POST[‘data’] as you said too but it didn’t work I don’t know why. :pensive:

I also don’t understand why the data value in the event “hook-sent” is “undefined” :

I will see in the future if I realy need to use json object.

Thank you again for your help CuriousTech

1 Like

The data json is nested in the main json. That makes it a little confusing. Plus it’s header data, which I though was in $_POST. But anyway, if you want, try something like…

$data = $_GET['data'] // returns the value associated with "data" from the _GET array
$json = json_decode($data, TRUE) // decodes to associative array
$temp = $json['temperature'] // returns value associated with 'temperature' in the array