Agent Does Nothing with my PHP JSON File

I have the following PHP script that creates a JSON file:

$count_users = 36;
$path = ‘ALCCount.json’;
// JSON data as an array
$jsonData = [
[
“count”=> $count_users
]
];

// Convert JSON data from an array to a string

$jsonString = json_encode($jsonData, JSON_PRETTY_PRINT);
// Write in the file
$fp = fopen($path, ‘w’);
//**** adding
fwrite($fp,“header(‘Content-Type: application/json;charset=utf-8’)”);
//*************
fwrite($fp, $jsonString);
fclose($fp);

The output of the JSON file looks as follows:

header(‘Content-Type: application/json;charset=utf-8’)[
{
“count”: 45
}
]

My Agent looks as follows:

local request=http.get(“http://www.XXXXXXX/CurrentOnlineIMP.php/ALCCount.json”,{ “Accept” : “application/json” } );
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");

}

});

The Agent does nothing with the incoming JSON just yields:

[Status] Agent restarted: reload.

What am I doing wrong?

Your first line creates a HTTP request object, but doesn’t send it.

You then need to call request.sendasync(function(response){ …
})

You only need to use http.onrequest when the agent must respond to HTTP from an external client.

Thank you for looking at my issue.
I see what you are saying but still at a loss on how to proceed.
Can you give a simple example?

First of all, I can help you out with the PHP.

You only need to save values (or a value) in a file, not the actual JSON array.
It’s the output that needs to be JSON encoded.

By saving only the values in your website file, you can use them for other things too.

So if you ran the PHP below, the file would contain 36
The output would be {“count”:36}
That’s what the agent will see when it accesses your PHP script.

Here is my PHP script example:

   $count_users = 36;
    $path = ‘ALCCount.json’;
	
    $fp = fopen($path, ‘w’);
     fwrite($fp, $count_users);
    fclose($fp);

// RETURN PARAMETERS AS JSON ARRAY
$arr = array("count" => $count_users);
echo json_encode($arr);

If you want to save multiple values in your website file, I usually separate the values by pipes |
Example: 36|John Doe|Feb 2, 2023

Then you can read that line from your file and split the values apart …
Example:

$filename="ALCCount.json";
$data=file($filename);
// pretend the file has this line:   36|John Doe|Feb 2, 2023
list($count,$name,$date)=explode("|",$data[0]);
// since your file will only have 1 line, you'll only read the first array $data[0].
// those 'list' variables will contain the data from that file as it explodes them apart by the pipes |

Now the Agent portion …

//------------------------------------------------------------------------------------------
// Request the user count from the remote PHP script
//---------------------------------------------------
// Basic wrapper to create an execute an HTTP POST
// You will call this Agent function from your device, whenever the device needs to read the user count
function getCount() {

// Set up outgoing request object
local request = http.get(“http://www.XXXXXXX/CurrentOnlineIMP.php/ALCCount.json”);

// Define the response handler
function handleResponse(responseTable) {
// Called when the imp receives a response from the remote service
if (responseTable.statuscode == 200) {
local response = http.jsondecode(responseTable.body);
// this is what the Agent sees from your PHP script: {“count”:36}
local countUsers = response.count;
// this will send the user count to the device
device.send(“countUsers”, countUsers);
} else {
// Log an error
server.log("Error response: " + responseTable.statuscode);
}
}
// Send the request asynchronously. This will not block the imp CPU
request.sendasync(handleResponse);
}
//------------------------------------------------------------------------------------------

If you want any more examples, or help with more, you can contact me.

This topic was automatically closed after 60 days. New replies are no longer allowed.