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条)

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?