I’m sending a json from a php script to my agent url, the url check if a particular key is good and then send the same json to the device,
How can I parse the json in the device… ?
PHP code :
`$_SESSION['config'] = array(
"secret" => $_SESSION['current_node_url'],
"I1Enabled" => xss_cleaner($_POST['I1Enabled']));
$data_string = json_encode($_SESSION['config']);
$ch = curl_init('https://agent.electricimp.com/'.$_SESSION['current_node_url']);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
$result = curl_exec($ch); `
Agent code
`http.onrequest(function(request, response){
try
{
// decode the json - if it's invalid json an error will be thrown
//server.log("body" + request.body);
local data = http.jsondecode(request.body);
if ("secret" in data) {
if(data.secret == knowngoodsecret )
{
response.send(200, "Got it ");
device.send("newconfig", request.body);
server.log("new config sent to device");
}
else
{
server.log("secret is bad");
response.send(400, "invalid request");
}
}
}
catch (e)
{
response.send(500, "Invalid JSON string");
}
});`
Device code
`function updatedeviceconfig(json)
{
server.log("received new config on device " + json);
}
// When we get a 'pong' message from the agent, call returnFromImp()
agent.on("newconfig", updatedeviceconfig);
`
I’m interested to know the code to put in my device to find the value of each element that are in the original array in my php script, for example I1Enabled …of course my real array is much much bigger ( 150 + parameters ) than what is shown here.