receiving xml file via http post
-
27-09-2019 - |
Question
I have a test script to receive an xml file via http post and it appears to work ok when I use it internally. When I move the script to a web server which can be accessed externally nothing appears to be happening. Anyone any ideas?
<?php
if ( $_SERVER['REQUEST_METHOD'] == 'POST' )
{
$inp = fopen("php://input");
$outp = fopen("xmlfile" . date("YmdHis") . ".xml", "w");
while (!feof($inp)) {
$buffer = fread($inp, 8192);
fwrite($outp, $buffer);
}
fclose($inp);
fclose($outp);
echo "<html><head>test response</head><body>OK</body></html>";
}
?>
To post the xml I'm using curl, not sure if this is the problem? and I'm NOT sending to a secure connection(HTTPS):
function httpsPost($Url, $xml_data)
{
//Initialisation
$ch=curl_init();
//Set parameters
curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1);
curl_setopt($ch, CURLOPT_URL, $Url);
//Return a variable instead of posting it directly
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//Activate the POST method
curl_setopt($ch, CURLOPT_POST, 1);
//Request
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_data);
curl_setopt($ch, CURLOPT_TIMEOUT, 4);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
//execute the connexion
$result = curl_exec($ch);
//Close it
curl_close($ch);
return $result;
}
Solution
Make sure that on your server allow_url_fopen
settings is turned on from php.ini.
Having said that, be aware of security concerns about that setting.
Update:
Try to see if there is any error, turn on error reporting, put these two lines on top of your script:
ini_set('display_errors', true);
error_reporting(E_ALL);
OTHER TIPS
Some other things to check:
php://input
isn't available if the form hasenctype=multipart/form-data
php://input
can only be read once (not likely, unless there's other parts to your script you haven't shown)- POST data size doesn't exceed Apache's
LimitRequestBody
and/or PHP'supload_max_size
/post_max_size
Any reason you have to read the raw input and can't do essentially fwrite($outp, $_POST['xml'])
?