Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   XML (http://www.velocityreviews.com/forums/f32-xml.html)
-   -   http post an xml-doc with php (http://www.velocityreviews.com/forums/t167048-http-post-an-xml-doc-with-php.html)

steven 05-06-2004 12:44 PM

http post an xml-doc with php
 
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

Martin Honnen 05-06-2004 01:42 PM

Re: http post an xml-doc with php
 
steven wrote:

> 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('aspProce ssed', now.toGMTString());

Response.ContentType = 'text/xml';

xmlDocument.save(Response);
%>
--

Martin Honnen
http://JavaScript.FAQTs.com/


All times are GMT. The time now is 11:43 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.