I have a PHP API hosted on a subdomain.
If I submit POST request from PHP the receiving API reads the JSON parameter.
When I submit from the command line it does not.
My PHP API call is this:
<?php
$url = "https://MySubdomain/MyAPIEndpoint.php";
$headerfields = array(
'Accept:application/json',
'Content-Type: application/json',
);
$postfields = array("orderid" => 123456);
$postfields = json_encode($postfields);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL,$url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER,true);
curl_setopt($curl, CURLOPT_POST,1);
curl_setopt($curl, CURLOPT_POSTFIELDS,$postfields);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headerfields);
$result = curl_exec($curl);
curl_close($curl);
?>
The command line call is this:
curl --header "Content-Type: application/json" --request POST --data "{"orderid":123456}" https://Mydomain/MyAPIEndPoint.php
The endpoint is reading the JSON passed with this:
$content = trim(file_get_contents("php://input"));
$decoded = (json_decode($content, true));
$orderid = $decoded['orderid'];
The PHP Curl POST works OK but the command line version is returning a warning Trying to access array offset on null
– which indicates that the parameter is not set and therefor not being read by my endpoint code.
The aim of this is to be able to build a command line CuRL command to allow me submit it as a ‘fire and forget’ call from an exec()
statement.
Spencer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.