如何从以下获取响应代码

时间:2012-05-02 05:07:56

标签: php xml soap

我需要获取ResponseCode和ResponseDescription我正在使用PHP

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <ProcessPaymentResponse xmlns="https://services.incard.com.au">
      <ProcessPaymentResult>
        <ResponseCode>string</ResponseCode>
        <ResponseDescription>string</ResponseDescription>
      </ProcessPaymentResult>
    </ProcessPaymentResponse>
  </soap:Body>
</soap:Envelope>

4 个答案:

答案 0 :(得分:0)

你需要php xml解析器。

使用simplexml

此处有更多信息:http://in2.php.net/manual/en/simplexml.examples.php

答案 1 :(得分:0)

您可以考虑使用php SoapClient,假设它们正在向其公开SOAP服务 你。

如果没有其他一些事情你可以做:

  1. 使用XPath获取值: http://php.net/manual/en/simplexmlelement.xpath.php

  2. 使用php的XML解析函数之一来解析XML并获取值:http://www.php.net/manual/en/function.xml-parse-into-struct.php

答案 2 :(得分:0)

试试这个:

<?php
$str = '<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <ProcessPaymentResponse xmlns="https://services.incard.com.au">
      <ProcessPaymentResult>
        <ResponseCode>string</ResponseCode>
        <ResponseDescription>string</ResponseDescription>
      </ProcessPaymentResult>
    </ProcessPaymentResponse>
  </soap:Body>
</soap:Envelope>';

$xml = simplexml_load_string( $str );
$xml->registerXPathNamespace( 'soap', 'http://schemas.xmlsoap.org/soap/envelope/' );
$result = $xml->xpath('//soap:Body');

foreach ($result as $body) {
  echo $body->ProcessPaymentResponse->ProcessPaymentResult->ResponseCode . "<br />";
  echo $body->ProcessPaymentResponse->ProcessPaymentResult->ResponseDescription . "<br />";
}
?>

希望这有帮助。

答案 3 :(得分:0)

in

如果你想要单个数据字段

,那么将新的.xml放入你的xml数据,然后执行下面的代码
 <?php

$file="new.xml";

function contents($parser, $data){ 
    echo $data; 
} 

function startTag($parser, $data){ 
    echo "<b>"; 
} 

function endTag($parser, $data){ 
    echo "</b><br />"; 
} 

$xml_parser = xml_parser_create(); 

xml_set_element_handler($xml_parser, "startTag", "endTag"); 

xml_set_character_data_handler($xml_parser, "contents"); 

$fp = fopen($file, "r"); 

$data = fread($fp, 80000); 

if(!(xml_parse($xml_parser, $data, feof($fp)))){ 
    die("Error on line " . xml_get_current_line_number($xml_parser)); 
} 

xml_parser_free($xml_parser); 

fclose($fp); 

$xml = simplexml_load_string( $data);
$xml->registerXPathNamespace( 'soap', 'http://schemas.xmlsoap.org/soap/envelope/' );
$result = $xml->xpath('//soap:Body');

foreach ($result as $body) {
  echo $body->ProcessPaymentResponse->ProcessPaymentResult->ResponseCode . "<br />";
  echo $body->ProcessPaymentResponse->ProcessPaymentResult->ResponseDescription . "<br />";
}
?>

如果你想要n个数据试试这个

<?php
$file="new.xml";

function contents($parser, $data){ 
    echo $data; 
} 

function startTag($parser, $data){ 
    echo "<b>"; 
} 

function endTag($parser, $data){ 
    echo "</b><br />"; 
} 

$xml_parser = xml_parser_create(); 

xml_set_element_handler($xml_parser, "startTag", "endTag"); 

xml_set_character_data_handler($xml_parser, "contents"); 

$fp = fopen($file, "r"); 

$data = fread($fp, 80000); 

if(!(xml_parse($xml_parser, $data, feof($fp)))){ 
    die("Error on line " . xml_get_current_line_number($xml_parser)); 
} 

xml_parser_free($xml_parser); 

fclose($fp); 
    $xml = simplexml_load_string($data); 
    $xml->registerXPathNamespace('soap:Envelope', 'http://schemas.xmlsoap.org/soap/envelope/');
    foreach ($xml->xpath('//soap:Envelope:ResponseCode') as $item) {
        echo (string) $item; 
    }
    foreach ($xml->xpath('//soap:Envelope:ResponseDescription') as $item) {
        echo (string) $item; 
    }
    ?>