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);
%>