I've been trying to create a connection between two hosts with a POST request sent as JSON using cURL in PHP. My primary objective is to send data from Host A to Host B (labelled for clarity).
First, looking at network tools in the sender browser, the request type is always GET instead of POST, despite using CURLOPT_POSTFIELDS and CURLOPT_POST. Secondly, the data is not printing/echoing in the receiving browser. However, the hardcoded outputs in the receiving script are printing in both browsers. I've tried using CURLOPT_CUSTOMREQUEST 'POST' to no avail.
Sender JSON tab: SyntaxError: JSON.parse: unexpected non-whitespace character after JSON data at line 1 column 59 of the JSON data
Sender Raw Data tab: {"ChangeType":"renamed","Path":"C:\File1","Param3":"Yes"}receive_jsonpost_test.php is running renamedC:\File1YesObject id #1
Code:
<?php
// HOST A (sender)
header("Content-Type:application/json");
$testobj = array(
'ChangeType' => 'renamed',
'Path' => 'C:\\File1',
'Param3' => 'Yes'
);
$url = '[redacted].edu/receive_jsonpost_test.php';
$ch = curl_init($url);
$payload = json_encode($testobj);
print($payload);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
$result = curl_exec($ch);
curl_close($ch);
print($result);
?>
<?php
// HOST B (recipient)
print('receive_jsonpost_test.php is running ');
$json = file_get_contents('php://input');
$data = json_decode($json);
print($data->ChangeType);
print($data->Path);
print($data->Param3);
print($data);
I know this is probably a pretty basic problem, but I'm fairly new to applied host-to-host communication and any help would be greatly appreciated. Thanks.
CodePudding user response:
If you're trying to interpret the response from Host A, ie...
print($result);
as JSON, you won't have much luck since Host B does not respond with JSON. Host B's code should look more like this...
$json = file_get_contents('php://input');
$data = json_decode($json);
// ...do whatever but no `echo` or `print`
header('content-type: application/json');
echo json_encode($someDataStructure); // now respond with some JSON
exit;
You should also remove this line from Host A as it too will invalidate a JSON response...
print($payload);
looking at network tools in the sender browser...
Your browser isn't making the request to Host B, PHP is. If your browser makes a GET request to Host A, then that's all it will see.
CodePudding user response:
If all you want to do is send data to HOST B and have B echo the received data back to HOST A:
Remove this from HOST A:
print($payload);
It will send headers to the Browser. The headers should come from HOST B response headers.
Same goes for
header("Content-Type:application/json");
Again, headers should come come from HOST B response headers.
HOST A request:
$postdata= array(
'ChangeType' => 'renamed',
'Path' => 'C:\\File1',
'Param3' => 'Yes'
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
$result = curl_exec($ch);
echo $response;
HOST B possible responses
header('Content-Type: text/plain');
foreach($_POST as $key => $value){
echo "$key $value\n"
}
OR If you want to support both POST and GET
foreach($_REQUEST as $key => $value){
echo "$key $value\n"
}
If you really need JSON:
$postdata= json_encode(array(
'ChangeType' => 'renamed',
'Path' => 'C:\\File1',
'Param3' => 'Yes'
));
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
curl_setopt($ch, CURLOPT_POST, true);
$result = curl_exec($ch);
echo $response;
HOST B
header('content-type: application/json');
echo file_get_contents('php://input');
OR
header('content-type: text/plain');
echo file_get_contents('php://input');
OR
header('content-type: text/plain');
var_export(json_decode(file_get_contents('php://input'),1);
If you want to do a GET request in a Browser:
https://example.com?ChangeType=renamed'&Path=C:\\File1&Param3=Yes
foreach($_REQUEST as $key => $value){
echo "$key $value\n"
}