Parse SOAP Response using Simple XML PHP

后端 未结 2 1511
时光取名叫无心
时光取名叫无心 2021-01-29 10:34

After passing SOAP response using simplexml i got the following out put. How can i get the value of attributes of domain ie, name and avail.

code used:

          


        
相关标签:
2条回答
  • 2021-01-29 11:06

    Apparently, you have escaped XML in the return (which is a bad practice I'll ignore for now..). Also, look into the children() function to work with namespaces instead of your preg_replace.... Ignoring that, this would work for you:

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

    On a side note, I usually use this tidbit to leverage SOAPClient to parse soap responses:

    //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());
    
    0 讨论(0)
  • 2021-01-29 11:19

    Inspired by @Wrikken's answer, I wrote a simple to use class that works with 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();
      }
    }
    

    Usage example:

    $soapParser = new SoapParser(array('soap_version' => 'SOAP_1_1'));
    try {
      var_dump($soapParser->parse($response));
    } catch (Exception $e) {
      die($e->getMessage());
    }
    
    0 讨论(0)
提交回复
热议问题