doushi3189 2017-01-25 22:57
浏览 140
已采纳

从PHP中的SOAP响应中提取值

I need to a value from this SOAP response. The value is in the loginresponse / return element. Here's the response:

<soap-env:envelope xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/" 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/">
<soap-env:body soap-env:encodingstyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns1="urn:DBCentralIntf-IDBCentral">
    <ns1:loginresponse>
        <return xsi:type="xsd:string"><**THIS IS THE VALUE I NEED**></return>
    </ns1:loginresponse>
</soap-env:body>

Here's how I'm trying to parse:

    $response = curl_exec($ch);
    curl_close($ch);

    $xml = simplexml_load_string($response, NULL, NULL, "http://schemas.xmlsoap.org/soap/envelope/");
    $ns = $xml->getNamespaces(true);
    $soap = $xml->children($ns['SOAP-ENV']);
    $res = $soap->Body->children($ns['NS1']);

    print_r($res->LoginResponse->Return);

But I get an empty object.

Thanks for your help!

  • 写回答

2条回答 默认 最新

  • duanqiu9104 2017-01-26 15:48
    关注

    Instead of using cURL and attempting to parse the XML response, consider using the PHP SOAP client. You may need to install PHP SOAP or enable it in your PHP configuration. (I'm using PHP on Windows, so I just had to uncomment extension=php_soap.dll in php.ini.)

    If you have SOAP installed, you can get the WSDL from the provider of the web service you're using. Based on Googling this value in the XML you showed: xmlns:ns1="urn:DBCentralIntf-IDBCentral", I'm guessing you can find it here, but you'll probably have better luck finding it since you know for sure what web service you're using.

    After you have the WSDL, using the PHP SOAP client is super easy:

    $client = new SoapClient('path/to/your.wsdl');
    $response = $client->Login(['username', 'password']);
    $theValueYouNeed = $response->loginresponse->return;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码
  • ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
  • ¥50 400g qsfp 光模块iphy方案
  • ¥15 两块ADC0804用proteus仿真时,出现异常
  • ¥15 关于风控系统,如何去选择