views:

41

answers:

2

Hi,

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;
 }
A: 

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);
Sarfraz
Hi, yeah it does appeear to be set allow_url_fopen = On
thegunner
@thegunner: See my update please.
Sarfraz
Hi, thanks. I've tried that appear to give any errors. basically the script on the webserver is only that code above + your update. Not sure what the prob is!
thegunner
edited the question to show how i was posting it...not sure if that could be a reason.
thegunner
Do you have the curl extension turned on in the first place?
Sarfraz
yeah...it does work for another web application and it was working when I post to localhost. When though I put e.g. echo "test1"; in the receiving script above nothing is being echoed out...so that's why I was wondering if it was being posted correctly. May try posting another way.
thegunner
Ok, when I view the folder via IIS all the files are showing. But they're not when I view them as you would a normal folder.
thegunner
ok got this sorted nothing to do with error or posting method. thanks for your help.
thegunner
A: 

Some other things to check:

  1. php://input isn't available if the form has enctype=multipart/form-data
  2. php://input can only be read once (not likely, unless there's other parts to your script you haven't shown)
  3. POST data size doesn't exceed Apache's LimitRequestBody and/or PHP's upload_max_size/post_max_size

Any reason you have to read the raw input and can't do essentially fwrite($outp, $_POST['xml'])?

Marc B