I am trying to post XML data to a remote asp server from within a PHP application and receive XML data back.

I have tried this:

function post_xml($server, $port, $url, $xml) {

$content_length = strlen($xml);
$headers = "POST $url HTTP/1.1
Accept: text/plain, text/html, text/xml, image/gif, image/jpeg,
image/png, image/bmp
Accept-Charset: UTF-8
Accept-Language: en
Content-Length: $content_length
Cache-Control: no-cache
Content-Type: text/xml
Host: $server
User-Agent: PHP

";
$fp = fsockopen($server, $port, $errno, $errstr);
if (!$fp) {
return false;
}
fputs($fp, $headers);
fputs($fp, $xml);

    $ret = ""; 
    while (!feof($fp)) 
        $ret.= fgets($fp, 1024); 
    fclose($fp); 
    return $ret; 

}

And this is the given VB example of how the posting is supposed to be done:


Dim xmlDoc
Dim xmlHTTP_
Dim ConfirmationXML

Set xmlDoc = CreateObject("Microsoft.XMLDOM")

xmlDoc.async = False
xmlDoc.Load “C:\Orders.XML”

Set xmlHTTP = CreateObject("Microsoft.XMLHttp")_

xmlHTTP.Open "POST", “http://orders.somepartner.com/Orders/SubmitOrders.asp”, False_

xmlHTTP.send xmlDoc.xml___

ConfirmationXML = xmlHTTP.responseXML.xml_

I am calling the above post_xml function with the appropriate server, path, and on port 80. But I have not found any help so far on how one is supposed to post XML to an asp server, and the above does not work - it just hangs and the server doesn't seem to be responding with anything or closing the port or something.... not really sure.

How is one ideally supposed to post an XML file to an ASP server from within PHP?

Any help would be greatly appreciated.

Thanks,

Mike

    I have made a VB script to send request to my own server using the VB code, and alalyzed what was sent. I've been working to make PHP send something similar as well, and I've found some clues...

    First, I've noticed that the server does not ever close the connection when you POST raw data. At least this is true of the remote server I've been working on, as well as my own test server. The server does send back the length of the response though. Unfortunately, it's not in a Content-Length header line. (I could probably dig it out of the HTTP specs to figure out exactly what is going on)

    The server sends back this:

    HTTP:/1.1 200 OK
    Date: Sat, 01 Nov 2003 19:01:01 GMT
    Server: Apache/1.3.27
    X-Powered-By: PHP/4.3.1
    Transfer-Encoding: chunked
    Content-Type: text/html

    21
    This is the response.

    Note how the length is onthe line when the content actually starts. It's not trivial to parse through that stuff on the fly and pull out the length, I am thinking.

    So you can't read until you get a connection close. You have to read until you get the right number of bytes.

    To avoid parsing the result on the fly and reading just the right number of bytes, I've noticed you can call stream_set_timeout($fp, 5) just before the fgets. This way, if the server stalls in sending its stuff for 5 seconds or so on a single line, you may (falsely but rarely) conclude you are done. Of course you have to detect that fgets gets a timeout error and stop the reading loop there or it won't do any good.

    Does this sound way off base to anyone more familiar with this topic?

    I'll write it up and post it once I have the solution, even if I have to iron it all out by myself.

    Thanks,

    Mike

    PS should this kind of question be here, or in the "coding" section?

      Write a Reply...