Sending XML through get with fopen

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
bdeonline
Forum Commoner
Posts: 42
Joined: Sun Jul 18, 2004 10:45 am

Sending XML through get with fopen

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

rawurlencode() the XML.
bdeonline
Forum Commoner
Posts: 42
Joined: Sun Jul 18, 2004 10:45 am

Post 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"
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

..and you're sure about the url being valid? no case issues?
bdeonline
Forum Commoner
Posts: 42
Joined: Sun Jul 18, 2004 10:45 am

Post 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;
Last edited by bdeonline on Mon Feb 21, 2005 4:18 pm, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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>'
bdeonline
Forum Commoner
Posts: 42
Joined: Sun Jul 18, 2004 10:45 am

Post 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.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post 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
Post Reply