-1

I have a xml request that looks like the code below and I would like to store in a PHP variable ONLY the value in the node RATE that should come in the Response.

How could I achieve that?

$zip = 90002;
$pounds = 0.1;
$shippingMode = "Express";
$url = "http://production.shippingapis.com/shippingAPI.dll";

$devurl ="testing.shippingapis.com/ShippingAPITest.dll";
$service = "RateV4";
$xml = rawurlencode("<RateV4Request USERID='USER' >
<Revision/>
     <Package ID='1ST'>
          <Service>$shippingMode</Service>
          <ZipOrigination>10025</ZipOrigination>
          <ZipDestination>".$zip."</ZipDestination>
          <Pounds>".$pounds."</Pounds>
          <Ounces>0</Ounces>
          <Container></Container>
          <Size>REGULAR</Size>
          <Width></Width>
          <Length></Length>
          <Height></Height>
          <Girth></Girth>
     </Package>
</RateV4Request>");
$request = $url . "?API=" . $service . "&xml=" . $xml;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$request);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_HTTPGET, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$result = curl_exec($ch);
curl_close($ch);

The response should be something like this. I just need to grab the RATE value.

<?xml version="1.0" encoding="UTF-8"?>
<RateV4Response>
     <Package ID="1ST">
          <ZipOrigination>44106</ZipOrigination>
          <ZipDestination>20770</ZipDestination>
          <Pounds>1</Pounds>
          <Ounces>8</Ounces>
          <Container>NONRECTANGULAR</Container>
          <Size>LARGE</Size>
          <Width>15</Width>
          <Length>30</Length>
          <Height>15</Height>
          <Girth>55</Girth>
          <Zone>3</Zone>
          <Postage CLASSID="1">
               <MailService>Priority Mail&lt;sup&gt;&amp;reg;&lt;/sup&gt;</MailService>
               <Rate>24.85</Rate>
          </Postage>
     </Package>
</RateV4Response>   

LATEST CODE

$zip = 90002;
$pounds = 0.1;
$shippingMode = "Express";
function USPSParcelRate($pounds,$zip,$shippingMode) {
$url = "http://production.shippingapis.com/shippingAPI.dll";

$devurl ="testing.shippingapis.com/ShippingAPITest.dll";
$service = "RateV4";
$userid = "023TAHAR4995";
$xml = rawurlencode("<RateV4Request USERID='023TAHAR4995' >
<Revision/>
     <Package ID='1ST'>
          <Service>$shippingMode</Service>
          <ZipOrigination>10025</ZipOrigination>
          <ZipDestination>".$zip."</ZipDestination>
          <Pounds>".$pounds."</Pounds>
          <Ounces>0</Ounces>
          <Container></Container>
          <Size>REGULAR</Size>
          <Width></Width>
          <Length></Length>
          <Height></Height>
          <Girth></Girth>
     </Package>
</RateV4Request>");
$request = $url . "?API=" . $service . "&xml=" . $xml;
// send the POST values to USPS
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$request);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_HTTPGET, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// parameters to post


$result = curl_exec($ch);
curl_close($ch);
$response = new SimpleXMLElement($result);

echo $response->RateV4Response->Package->Postage->Rate;

}
USPSParcelRate($pounds,$zip, $shippingMode);
samyb8
  • 2,560
  • 10
  • 40
  • 68
  • use simpleXML...http://www.w3schools.com/php/php_xml_simplexml.asp – Kylie Jun 11 '13 at 15:46
  • But how would I grab the response from the above REQUEST so I can apply the `new SimpleXMLElement($response)`? – samyb8 Jun 11 '13 at 16:01
  • well you have the result in the code already...$result = curl_exec($ch); ?? just pass that $result into simpleXML...see below... – Kylie Jun 11 '13 at 16:05
  • Start to read the PHP manual: http://php.netsimplexml.examples-basic Also start to search the website, such a general question has been asked and answered before. – hakre Jun 11 '13 at 18:00
  • @KyleK , please never **EVER** direct a newbie to w3schools site. It is considered harmful. – tereško Jun 14 '13 at 23:07
  • Really??? Why is that @teresko? – Kylie Jun 14 '13 at 23:21
  • @teresko Also....if a newbie uses google, wouldn't that likely be the first place they end up anyways?? Please inform me as to why its bad.....do the propogate bad practices? – Kylie Jun 14 '13 at 23:58
  • @KyleK , this should explain it: http://w3fools.com/ – tereško Jun 15 '13 at 07:55

1 Answers1

1

Heres a good tutorial on the matter...

http://blog.teamtreehouse.com/how-to-parse-xml-with-php5

It would go something like this...probably not working right away, as I haven't really digested the structure of your xml...just glanced at it....but something along these lines... :)

    $request = $url . "?API=" . $service . "&xml=" . $xml;
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$request);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_HTTPGET, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $result = curl_exec($ch);

    $XML = new SimpleXMLElement($result);

     foreach($XML->Package as $val){
     echo $val->Postage->Rate;
         }

Or if its always just one Response/Package node....then you can just get the rate like this...

  echo $XML->Pacakge->Postage->Rate;
Kylie
  • 11,421
  • 11
  • 47
  • 78
  • It's printing nothing... I updated the post with the new code – samyb8 Jun 11 '13 at 16:23
  • what happens if you var_dump($response)? and don't close_curl before that – Kylie Jun 11 '13 at 16:23
  • Are you sure that your response is that XML file?? what happens when you var_dump($result) after the $result = curl_exec($ch); – Kylie Jun 11 '13 at 16:27
  • This page might help, but theres not much different between it and my answer...http://stackoverflow.com/questions/561816/php-curl-extract-an-xml-response – Kylie Jun 11 '13 at 16:28
  • var_dump returns " string(405) " 10025900020.10VARIABLEREGULAR8Express Mail<sup>&reg;</sup>30.60" – samyb8 Jun 11 '13 at 16:33
  • Heres a whole set of PHP wrapper classes written for the USPS service.....maybe this could help you? https://github.com/VinceG/USPS-php-api – Kylie Jun 11 '13 at 16:44
  • Thanks. Not sure I can find the answer to my issue in here, I will try somewhere else. – samyb8 Jun 11 '13 at 17:01
  • The docs at USPS are quite helpful, Im reading them now...I suggest doing that – Kylie Jun 11 '13 at 17:02
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/31600/discussion-between-samyb8-and-kylek) – samyb8 Jun 11 '13 at 17:10