0

我一直在研究 Saia Carrier API,它给了我一些价值,尽管它们不是很吸引人。我想解析这些值并将它们放入变量中,以便可以在表格中整齐地显示它们。

这是代码:

<?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);

?>
4

2 回答 2

0

我一直使用 PHP 的 SimpleXML 库: http: //php.net/manual/en/book.simplexml.php

这创建了一个易于使用和遍历的非常基本的对象。

children()这是使用该站点的函数遍历的示例: 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'];
        }
    }
}
?>

您还可以通过attributes()函数检索属性: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";
}
?>
于 2013-09-05T20:27:38.787 回答
0

我知道了。

刚刚添加:

 $xml = simplexml_load_string($response); 

并改变:

print_r($response);

至:

 print_r($xml);
于 2013-09-05T20:30:46.187 回答