Forum Moderators: coopster

Message Too Old, No Replies

Using PHP to POST XML to remote server

this is so customer on e-commerce site can get rates and service from UPS

         

jakefree24

11:17 pm on Jan 15, 2007 (gmt 0)

10+ Year Member



I got all these XML templates from UPS to post relevant shipping data from our site to UPS's so they can calculate shpping costs then POST it back to us so we can add it to the sale price. I understand the templates well enough, but how do you use the HTTP POST to get the XML to UPS's site? I can't find one example of this anywhere. Then I found some javascript stuff on W3Schools, using the XMLHttpRequest & Microsoft.XMLHTTP objects, but those JS objects won't work in any of my browsers. Please give me a hint. I am pretty good with FORMS and PHP and HTML but new to XML.
We have to be able to read into the page values like TotalWeight and ZipCode for the UPS to use

scriptmasterdel

12:11 am on Jan 16, 2007 (gmt 0)

10+ Year Member



Howdy,

Could you please give me some examples of the data you would like to be sent and how you asume the external website would handle that data?

Also, what browser are you using for XMLHttpRequest & Microsoft.XMLHTTP not to work?

I hope i can help you out soon ;-)

Del

barns101

1:48 pm on Jan 16, 2007 (gmt 0)

10+ Year Member



Here is some exemplified code that I use to post an XML packet to an SMS text message provider and receive a response. I've stripped a lot of it out and not checked it for errors, but hopefully it'll at least get you started.


<?php
$fp = fsockopen("www.example.com", 80, $errno, $errstr, 30);
if (!$fp)
{
echo 'Could not open connection.';
}
else
{
$xmlpacket ='<?XML version="1.0"?>
<Your_XML>
</Your_XML>';

$contentlength = strlen($xmlpacket);

$out = "POST /script_name.php HTTP/1.0\r\n";
$out .= "Host: www.example.com\r\n";
$out .= "Connection: Keep-Alive\r\n";
$out .= "Content-type: application/x-www-form-urlencoded\r\n";
$out .= "Content-length: $contentlength\r\n\r\n";
$out .= "XML=$xmlpacket";

fwrite($fp, $out);
while (!feof($fp))
{
$theOutput .= fgets($fp, 128);
}
fclose($fp);

// $theOutput is the response returned from the remote script
}
?>