【问题标题】:Accessing a specific XML node with PHP [duplicate]使用 PHP 访问特定的 XML 节点 [重复]
【发布时间】:2013-06-07 13:08:03
【问题描述】:

我有一个类似于下面代码的 xml 请求,我想在 PHP 变量中仅存储应该在响应中出现的节点 RATE 中的值。

我怎样才能做到这一点?

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

响应应该是这样的。我只需要获取 RATE 值。

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

最新代码

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

【问题讨论】:

  • 但是我如何从上述请求中获取响应,以便我可以应用new SimpleXMLElement($response)
  • 你已经在代码中得到了结果...$result = curl_exec($ch); ??只需将 $result 传递给 simpleXML...见下文...
  • 开始阅读PHP手册:php.netsimplexml.examples-basic 也开始搜索网站,这样一个笼统的问题之前已经问过和回答过。
  • @KyleK,请永远不要永远将新手引导到 w3schools 网站。它被认为是有害的。

标签: php xml variables request


【解决方案1】:

这是一个很好的教程...

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

它会像这样......可能无法立即工作,因为我还没有真正消化你的 xml 的结构......只是看了一眼......但是沿着这些思路......: )

    $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;
         }

或者如果它总是只有一个响应/包节点......那么你可以得到这样的速率......

  echo $XML->Pacakge->Postage->Rate;

【讨论】:

  • 它什么也没打印...我用新代码更新了帖子
  • 如果你使用 var_dump($response) 会发生什么?并且在此之前不要 close_curl
  • 您确定您的响应是那个 XML 文件吗? var_dump($result) 在 $result = curl_exec($ch); 之后会发生什么
  • 此页面可能会有所帮助,但它与我的答案并没有太大区别...stackoverflow.com/questions/561816/…
  • var_dump 返回 "string(405)" 10025900020.10VARIABLEREGULAR8Express Mail<sup>&reg;</sup>30.60"
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-05-26
  • 2013-12-30
  • 1970-01-01
  • 1970-01-01
  • 2017-05-24
相关资源
最近更新 更多