0

I have been working on the Saia Carrier API and have it giving me values albeit they are not very appealing. I would like to parse the values and put them into variables so I can display them neatly in a table.

Here is the code:

<?php

$postdata = '<?xml version="1.0" encoding="utf-8"?>
<Create>
<UserID>XXX</UserID>
<Password>XXX</Password>
<TestMode>Y</TestMode>
<BillingTerms>Prepaid</BillingTerms>
<AccountNumber>XXX</AccountNumber>
<Application>Outbound</Application>
 <OriginZipcode>44483</OriginZipcode>
 <DestinationZipcode>90077</DestinationZipcode>
 <Details>
     <DetailItem>
        <Weight>100</Weight>
        <Class>85</Class>
    </DetailItem>
 </Details>
</Create>';


  $url = "http://www.saiasecure.com/webservice/ratequote/xml.aspx";

  $ch = curl_init($url);

  curl_setopt($ch, CURLOPT_POST, true);
  curl_setopt($ch, CURLOPT_POSTFIELDS,$postdata);
  curl_setopt($ch, CURLOPT_HTTPHEADER,array('Content-type: text/xml'));
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  $response = curl_exec($ch);
                 $info = curl_getinfo($ch);
  curl_close ($ch);

                       $status = $info['http_code'];

                echo $status;
                    echo '<pre>';
                print_r($response);

?>
2
  • 1
    It's XML, just parse it. You practically answered this yourself ^^ Commented Sep 5, 2013 at 20:24
  • possible duplicate of php parse xml string Commented Sep 5, 2013 at 20:24

2 Answers 2

0

I got it.

Just added:

 $xml = simplexml_load_string($response); 

and changed:

print_r($response);

to:

 print_r($xml);
Sign up to request clarification or add additional context in comments.

Comments

0

I've always used PHP's SimpleXML library: http://php.net/manual/en/book.simplexml.php

This creates a pretty basic object that is easy to use and traverse.

This is an example of traversing with the children() function from the site: http://www.php.net/manual/en/simplexmlelement.children.php

<?php
$xml = new SimpleXMLElement(
'<person>
 <child role="son">
  <child role="daughter"/>
 </child>
 <child role="daughter">
  <child role="son">
   <child role="son"/>
  </child>
 </child>
</person>');

foreach ($xml->children() as $second_gen) {
    echo ' The person begot a ' . $second_gen['role'];

    foreach ($second_gen->children() as $third_gen) {
        echo ' who begot a ' . $third_gen['role'] . ';';

        foreach ($third_gen->children() as $fourth_gen) {
            echo ' and that ' . $third_gen['role'] .
                ' begot a ' . $fourth_gen['role'];
        }
    }
}
?>

You can also retrieve attributes via the attributes() function: http://www.php.net/manual/en/simplexmlelement.attributes.php

<?php
$string = <<<XML
<a>
 <foo name="one" game="lonely">1</foo>
</a>
XML;

$xml = simplexml_load_string($string);
foreach($xml->foo[0]->attributes() as $a => $b) {
    echo $a,'="',$b,"\"\n";
}
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.