0

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

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

4 回答 4

0

你需要 php xml 解析器。

采用simplexml

更多信息在这里:http: //in2.php.net/manual/en/simplexml.examples.php

于 2012-05-02T05:14:33.023 回答
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

于 2012-05-02T05:17:43.740 回答
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 />";
}
?>

希望这可以帮助。

于 2012-05-02T05:27:40.160 回答
0

新的 .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; 
    }
    ?>
于 2012-05-02T06:06:28.047 回答