Jump to content

sending xml file through post: OK on localhost but not DEV


blinks

Recommended Posts

Using the following code on my localhost, I can successfully send an XML file to a specific remote server, and have a response returned. However, the same code moved up to a development server (with appropriate changes to $filename from localhost to dev.domain.etc), yields no response. If I rejig the code so that it reads from an XML file on my localhost rather than a POSTed XML, all works fine on DEV. Here's the code -

<?php
// based on example at http://en.allexperts.com/q/PHP5-3508/2008/12/Regarding-Sending-XML.htm

$data = file_get_contents("xmlexampledev.xml");

$arr= array('xmlparam' => $data);

$array= http_build_query($arr);
  
$context_options = array
(

      'http' => array (
          'method' => 'POST',
          'header'=> "Content-type: application/x-www-form-urlencoded
"
              . "Content-Length: " . strlen($array) . "
",
          'content' => $array
          )
      );
$filename = "http://dev.domain.com/pid/getpids.php";
//$filename = "http://localhost/pid/getpids.php";  // works
$context = stream_context_create($context_options);
$response = file_get_contents($filename, false, $context);
print_r($response);
?>

 

Here's the bit of code that grabs the XML POST -

$xmlstr = $_POST['xmlparam'];  
//if testing via direct file load, use below instead of $_POST above to load xml file as string
//$xmlstr = file_get_contents(XML_SOURCE_FILENAME);
//convert $xmlstring into SimpleXMLElement
$xml = new SimpleXMLElement($xmlstr); 

 

TIA

Solved the problem.

 

The calling code is now -

<?php
//http://www.usenet-forums.com/php-language/21143-need-post-xml-file-url.html

$xmlfile = file_get_contents("xmlexampledev.xml");

$alternate_opts = array(
'http'=>array(
'method'=>"POST",
'header'=>"Content-type: application/x-www-form-urlencoded\r\n" .
"Content-length: " . strlen("$xmlfile"),
'content'=>"$xmlfile"
)
);
$context = stream_context_create($alternate_opts);

$fp = fopen('http://www.domain.com/pid/getpids.php', 'r', false, $context);
fpassthru($fp);
fclose($fp);
?>

 

and the bit that grabs the POST is -

$xmlstr = file_get_contents("php://input"); 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.