【问题标题】:Parse SOAP Response using Simple XML PHP使用简单 XML PHP 解析 SOAP 响应
【发布时间】:2012-02-29 15:40:50
【问题描述】:

使用 simplexml 传递 SOAP 响应后,我得到了以下输出。如何获取域的属性值,即名称和可用性。

使用的代码:

 $xmlString = preg_replace("/(<\/?)(\w+):([^>]*>)/", "$1$2$3", $result);    
       $xml = simplexml_load_string($xmlString);
       print_r($xml);



SimpleXMLElement Object([soapBody] => SimpleXMLElement Object ([CheckAvailabilityResponse] => SimpleXMLElement Object([CheckAvailabilityResult] => &lt;?xml version="1.0" encoding="UTF-16"?&gt;
&lt;check&gt;
    &lt;domain name="MYNAMEISNIJIL.COM" avail="1" canBackorder="0"/&gt;
&lt;/check&gt;) ))

【问题讨论】:

    标签: php


    【解决方案1】:

    显然,您已经在返回中转义了 XML(这是一种不好的做法,我现在将忽略......)。另外,请查看 children() 函数以使用命名空间而不是您的 preg_replace.... 忽略这一点,这对您有用:

      $outerxml = simplexml_load_string($xmlString);
      $innerxml = simplexml_load_string( htmlspecialchars_decode(
         $outerxml->soapBody->CheckAvailabilityResponse->CheckAvailabilityResult));
    

    顺便说一句,我通常使用这个花絮来利用SOAPClient 来解析soap 响应:

    //the soap way
    class SneakyFauxSoap extends SoapClient {
        public $response;
        function __doRequest($val){
            return $this->response;
        }
    }
    
    $soap = new SneakyFauxSoap(null,
        array(
            'uri' =>'something',
            'location'=>'something',
            'soap_version' => SOAP_1_1));
    $soap->response = $x;
    var_dump($soap->somerandomfunction());
    

    【讨论】:

      【解决方案2】:

      受@Wrikken 回答的启发,我编写了一个简单易用的类 使用 PHP 5.3:

      class SoapParser extends SoapClient {
        private $xml;
      
        function __construct($options) {
          $options['location'] = $options['uri'] = 'dummy';
          parent::__construct(null, $options);
        }
      
        public function __doRequest($request, $location, $action, $version,
                                    $one_way = 0)
        {
          return $this->xml;
        }
      
        public function parse($xml) {
          $this->xml = $xml;
          return $this->dummyFunction();
        }
      }
      

      使用示例:

      $soapParser = new SoapParser(array('soap_version' => 'SOAP_1_1'));
      try {
        var_dump($soapParser->parse($response));
      } catch (Exception $e) {
        die($e->getMessage());
      }
      

      【讨论】:

        猜你喜欢
        • 2016-09-03
        • 1970-01-01
        • 2016-07-04
        • 2020-02-07
        • 1970-01-01
        • 1970-01-01
        • 2014-03-26
        • 1970-01-01
        • 2021-06-07
        相关资源
        最近更新 更多