I want to send JSON data to the agent from my PHP script.
The agent doesn’t like the JSON and says “Invalid JSON String”.
Here is my agent:
`//-----------------------------------------------------
// AGENT IN Receiving Data from Website
// ----------------------------------------------------
http.onrequest(function(request, response){
try
{
// decode the json - if it's invalid json an error will be thrown
local incoming = http.jsondecode(request.body);
server.log("Incoming Data: " + incoming.data);
// send response to whoever hit the agent url
response.send(200, "OK");
}
catch (e) {
// send a response indicating invalid JSON
response.send(500, "Invalid JSON string");
}
});`
And this is my PHP script for testing:
`<?php
$data = array("mykey" => "CP376aIe4MeSTwG", "relay1" => 1, "relay2" => 0);
$data_string = json_encode($data);
$ch = curl_init('https://agent.electricimp.com/Kj8Ytg748Rc/');
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);
echo $result;
?>`
Does anyone have more information about this?
My PHP script can POST to other PHP scripts and everything is valid JSON.
But a POST to the agent … the agent doesn’t like it.