http post an xml-doc with php

S

steven

Hallo,


I want to post an xml-doc to an URL using PHP. How can I do that ?

//$dom is the xml dom which I want to send.
$dom=domxml_open_file("example.xml");


Thanks,

Steven
 
M

Martin Honnen

steven said:
I want to post an xml-doc to an URL using PHP. How can I do that ?

//$dom is the xml dom which I want to send.
$dom=domxml_open_file("example.xml");

You can open a socket connection on the HTTP port (usually 80) and then
use fputs to write a HTTP POST request with the data, here is an example
doing that:

<?php
function postXMLToURL ($server, $path, $xmlDocument) {
$xmlSource = $xmlDocument->dump_mem();
$contentLength = strlen($xmlSource);
$fp = fsockopen($server, 80);
fputs($fp, "POST $path HTTP/1.0\r\n");
fputs($fp, "Host: $server\r\n");
fputs($fp, "Content-Type: text/xml\r\n");
fputs($fp, "Content-Length: $contentLength\r\n");
fputs($fp, "Connection: close\r\n");
fputs($fp, "\r\n"); // all headers sent
fputs($fp, $xmlSource);
$result = '';
while (!feof($fp)) {
$result .= fgets($fp, 128);
}
return $result;
}

function getBody ($httpResponse) {
$lines = preg_split('/(\r\n|\r|\n)/', $httpResponse);
$responseBody = '';
$lineCount = count($lines);
for ($i = 0; $i < $lineCount; $i++) {
if ($lines[$i] == '') {
break;
}
}
for ($j = $i + 1; $j < $lineCount; $j++) {
$responseBody .= $lines[$j] . "\n";
}
return $responseBody;
}

$xmlDocument = domxml_open_file('test20040506.xml');

$result = postXMLtoURL("localhost", "/javascript/test20040506.asp",
$xmlDocument);

$responseBody = getBody($result);

$resultDocument = domxml_open_mem($responseBody);

header('Content-Type: text/xml');
echo $resultDocument->dump_mem();
?>

That example loads a document, then posts it to an ASP page and receives
and XML document back which is then send to the browser.
My example ASP page simply puts a date stamp attribute on the XML and
sends it back to the PHP but of course you could do other things as needed:

<%@ Language="JScript" %>
<%
var xmlDocument = Server.CreateObject("Msxml2.DOMDocument.4.0");
xmlDocument.async = false;
xmlDocument.load(Request);

var now = new Date();
xmlDocument.documentElement.setAttribute('aspProcessed', now.toGMTString());

Response.ContentType = 'text/xml';

xmlDocument.save(Response);
%>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top