簡體   English   中英

如何解析 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 Client,而不是此處建議的 simple_xml_parser How to parse 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? 下面的代碼

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM