2013-06-11 2 views
-1

아래 코드와 같은 XML 요청이 있는데 응답에 포함되어야하는 노드 RATE의 값만 PHP 변수에 저장하고 싶습니다.PHP를 사용하여 특정 XML 노드에 액세스

어떻게하면됩니까?

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

사용 SimpleXML을을이 같은 속도를 얻을 수 있습니다 ... http : //www.w3schools.com/php/php_xml_simplexml.asp – KyleK

+0

그러나 위의 요청에서 응답을 받아 'new SimpleXMLElement ($ response)'를 적용 할 수 있습니까? – samyb8

+0

코드에 이미 결과가 있습니다 ... $ result = curl_exec ($ ch); ?? 그냥 $ 결과를 simpleXML로 전달하십시오 ... 아래를보십시오 ... – KyleK

답변

1

가 Heres는이 문제에 좋은 튜토리얼 ...

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

그것은 이런 식으로 뭔가를 갈 것 ... 아마 작동하지 최신 CODE 지금 당장 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; 
+0

인쇄 중입니다 ... 새 코드 – samyb8

+0

으로 게시물을 업데이트했습니다. var_dump ($ response)가 발생하면 어떻게됩니까? 그 전에 close_curl을하지 마십시오 – KyleK

+0

응답이 XML 파일입니까? $ result = curl_exec ($ ch) 뒤의 var_dump ($ result); – KyleK

관련 문제