我有一个看起来像下面代码的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<sup>&reg;</sup></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);
答案 0 :(得分: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;
}
或者如果它总是只有一个Response / Package节点....那么你可以得到这样的速率......
echo $XML->Pacakge->Postage->Rate;