如何解析SOAP响应


How to parse SOAP Response

我有下面的字符串,我想从这个字符串中解析用户名和密码。。。

 $xmlstring='<soap-env:envelope xmlns:ns1="http://back.com/"
            xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
            xmlns:soap-enc="http://schemas.xmlsoap.org/soap/encoding/" 
            xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/">
            <soap-env:header></soap-env:header>
            <soap-env:body>
               <ns1:createuserresponse>
                  <username>qqq_d481</username>
                  <password>sdfdssdfds</password>
                  <result>
                     <succeeded>true</succeeded>
                     <errorcode>0</errorcode>
                     <errortext></errortext>
                  </result>
               </ns1:createuserresponse>
            </soap-env:body>
           </soap-env:envelope>';

请提出建议。

上面的字符串是SOAP响应

但如果我使用:

$xmlstring='<soap-env:envelope>
<soap-env:header></soap-env:header>
<soap-env:body>
    <ns1:createuserresponse>
        <username>qqq_d481</username>
        <password>sdfdssdfds</password>
        <result>
            <succeeded>true</succeeded>
            <errorcode>0</errorcode>
            <errortext></errortext>
        </result>
    </ns1:createuserresponse>
</soap-env:body></soap-env:envelope>';

echo $xmltoparse= $xmlstring;
$xml = simplexml_load_string($xmltoparse);
print_r($xml); 

那么它工作得很好

也许您可以使用PHP5 Soap客户端,而不是simple_xml_parser如这里所建议的如何解析SOAP XML?

我不确定我是否完全理解你想做什么,但可能是这样的事情?

**未经测试的代码,但可能有助于

public function parseLoginResponse($loginResponse)
{
    $match = array(
        'username' => 'username',
        'password' => 'password',
        'succeeded' => 'succeeeded',
        'errortext' => 'errortext'
    );
    $this->_match($match, $loginResponse);
    return array(
        'user' => $this->username,
        'pass' => $this->password,
        'success' => $this->succeeded,
        'error' => $this->errortext
    );
}

尝试使用以下asXML函数将从中获得一个xml格式的字符串

print_r(htmlentities($xmldata->asXML());

有关更多信息,请查看以下链接

Simplexml_load_string($string)返回一个空对象,但$string包含xml?下方的代码