Page 1 of 1

Sending XML through get with fopen

Posted: Mon Feb 21, 2005 3:32 pm
by bdeonline
I am trying to send xml to usps to retreive shipping rates for a shopping cart. But I am having a hard time getting the request out.

Code: Select all

$filename = "http://testing.shippingapis.com/ShippingAPITest.dll?API=RateV2&XML=<RateV2Request USERID="test" PASSWORD="test"><Package ID="0"><Service>PRIORITY</Service><ZipOrigination>10022</ZipOrigination><ZipDestination>20008</ZipDestination><Pounds>10</Pounds><Ounces>5</Ounces><Container>Flat Rate Box</Container><Size>REGULAR</Size></Package></RateV2Request>";

$xml = simplexml_load_file($filename);
echo $xml->RateV2Response&#1111;0]->Rate;
It keeps giving me a 400 Bad Request.

Posted: Mon Feb 21, 2005 3:33 pm
by feyd
rawurlencode() the XML.

Posted: Mon Feb 21, 2005 3:58 pm
by bdeonline

Code: Select all

<?php
$filename = "http://testing.shippingapis.com/ShippingAPITest.dll?API=RateV2&XML=" . rawurlencode('<RateV2Request USERID="877BACK46071" PASSWORD="409BK65DW159"><Package ID="0"><Service>PRIORITY</Service><ZipOrigination>10022</ZipOrigination><ZipDestination>20008</ZipDestination><Pounds>10</Pounds><Ounces>5</Ounces><Container>Flat Rate Box</Container><Size>REGULAR</Size></Package></RateV2Request>');

$xml = simplexml_load_file($filename);
echo $xml->RateV2Response&#1111;0]->Package&#1111;0]->Postage&#1111;0]->Rate;
?>
Nope still gives me "HTTP request failed! HTTP/1.1 400 Bad Request"

Posted: Mon Feb 21, 2005 4:03 pm
by feyd
..and you're sure about the url being valid? no case issues?

Posted: Mon Feb 21, 2005 4:08 pm
by bdeonline
yea if you put the link in the browser it works just fine.

I added htmlspecialchars and now it comes up with no errors but no rate. Here is what it sends back do I have my simple xml set right to read it:

Code: Select all

<?xml version="1.0"?>
<RateV2Response>
<Package ID="0">
<ZipOrigination>10022</ZipOrigination>
<ZipDestination>20008</ZipDestination>
<Pounds>10</Pounds>
<Ounces>5</Ounces>
<Container>Flat Rate Box</Container>
<Size>REGULAR</Size>
<Zone>3</Zone>
<Postage>
<MailService>Priority Mail Flat Rate Box (11.25" x 8.75" x 6")</MailService>
<Rate>7.70</Rate>
</Postage>
<Postage>
<MailService>Priority Mail Flat Rate Box (14" x 12" x 3.5")</MailService>
<Rate>7.70</Rate>
</Postage>
</Package>
</RateV2Response>

Code: Select all

$xml->RateV2Response&#1111;0]->Package&#1111;0]->Postage&#1111;0]->Rate;

Posted: Mon Feb 21, 2005 4:17 pm
by feyd
on my machine

Code: Select all

<?php

$data = file_get_contents('http://testing.shippingapis.com/ShippingAPITest.dll?API=RateV2&XML=' . rawurlencode('<RateV2Request USERID="877BACK46071" PASSWORD="409BK65DW159"><Package ID="0"><Service>PRIORITY</Service><ZipOrigination>10022</ZipOrigination><ZipDestination>20008</ZipDestination><Pounds>10</Pounds><Ounces>5</Ounces><Container>Flat Rate Box</Container><Size>REGULAR</Size></Package></RateV2Request>'));

var_export($data);

?>
outputs:

Code: Select all

'<?xml version="1.0"?>
<RateV2Response><Package ID="0"><ZipOrigination>10022</ZipOrigination><ZipDestination>20008</ZipDestination><Pounds>10</Pounds><Ounces>5</Ounces><Container>Flat Rate Box</Container><Size>REGULAR</Size><Zone>3</Zone><Postage><MailService>Priority Mail Flat Rate Box (11.25" x 8.75" x 6")</MailService><Rate>7.70</Rate></Postage><Postage><MailService>Priority Mail Flat Rate Box (14" x 12" x 3.5")</MailService><Rate>7.70</Rate></Postage></Package></RateV2Response>'

Posted: Mon Feb 21, 2005 4:21 pm
by bdeonline
Yea thats working here as well so is it just "simplexml_load_file". Not sending the request right or can it not do remote request.

Posted: Mon Feb 21, 2005 6:49 pm
by timvw
here is a very simple client that posts your xml to the service... and returns everything that is sent by the server...

http://timvw.madoka.be/programming/php/client.txt