http post an xml-doc with php

Discussion in 'XML' started by steven, May 6, 2004.

  1. steven

    steven Guest

    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
     
    steven, May 6, 2004
    #1
    1. Advertising

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

    Response.ContentType = 'text/xml';

    xmlDocument.save(Response);
    %>
    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, May 6, 2004
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matt
    Replies:
    3
    Views:
    541
    Tor Iver Wilhelmsen
    Sep 17, 2004
  2. designsimply

    Can I http post xml using php?

    designsimply, May 16, 2005, in forum: XML
    Replies:
    5
    Views:
    30,625
    designsimply
    May 16, 2005
  3. Replies:
    6
    Views:
    771
    John J. Lee
    Jun 25, 2006
  4. CindyH
    Replies:
    1
    Views:
    642
    Alvin Bruney [ASP.NET MVP]
    May 14, 2008
  5. Rajive Narain
    Replies:
    0
    Views:
    2,016
    Rajive Narain
    Sep 18, 2009
Loading...

Share This Page