dragon7713 2014-03-28 12:24
浏览 47
已采纳

PHP解析SOAP响应[重复]

This question already has an answer here:

I'm trying to parse the following SOAP response, and need some guidance:

<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
    <env:Header/>

    <env:Body>
        <ns2:LookupResponse xmlns:ns2="http://path-to/schemas">
        <ns2:Name>My Name</ns2:Name>
        <ns2:Address1>test</ns2:Address1>
        <ns2:Address2>test</ns2:Address2>
        ...
        </ns2:LookupResponse>
    </env:Body>
</env:Envelope>

I retreive the response via cURL:

$url        = 'https://path-to-service';
$success    = FALSE;
$ch         = curl_init();

curl_setopt($ch, CURLOPT_URL,           $url);                                                                
curl_setopt($ch, CURLOPT_RETURNTRANSFER,    true);  
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,    false);
curl_setopt($ch, CURLOPT_SSLVERSION,        3);
curl_setopt($ch, CURLOPT_POST,          true);
curl_setopt($ch, CURLOPT_POSTFIELDS,        $request);
curl_setopt($ch, CURLOPT_HTTPHEADER,        array(
                                                    'Content-Type: text/xml; charset=utf-8', 
                                                    'Content-Length: ' . strlen($request) 
                                                ));

$ch_result  = curl_exec($ch);
$ch_error   = curl_error($ch);

curl_close($ch);

I'm new to all of this, so forgive obvious errors, but I am then trying to iterate through the response as an object, as parsed by the simpleXML extension, referencing the SO answer here and using the simplexml_debug plugin to echo object content.

if(empty($ch_error))
{
    $xml    = simplexml_load_string($ch_result, NULL, NULL, "http://schemas.xmlsoap.org/soap/envelope/");
    $xml    ->registerXPathNamespace('env', 'http://schemas.xmlsoap.org/soap/envelope/');
    $xml    ->registerXPathNamespace('ns2', 'http://path-to/schemas');


    echo '<pre>';
    simplexml_dump($xml);
    echo '</pre>';

}
else
{
    echo 'error';
    show($ch_error);
    exit;
}

This gives me the following:

SimpleXML object (1 item)
[
Element {
    Namespace: 'http://schemas.xmlsoap.org/soap/envelope/'
    Namespace Alias: 'env'
    Name: 'Envelope'
    String Content: ''
    Content in Namespace env
        Namespace URI: 'http://schemas.xmlsoap.org/soap/envelope/'
        Children: 2 - 1 'Body', 1 'Header'
        Attributes: 0
}
]

I want to get to the stage where I can iterate through the Body of the XML document, using either a foreach loop, or merely pointing directly to the relevant data ($title = (string)$data->title;). How can I proceed from my current position to get to that stage? I don't really know what comes next, and I just flat-out don't understand the documentation provided for the SOAP extension in PHP. I would rather use 'basic' code to achieve what I need.

</div>
  • 写回答

1条回答 默认 最新

  • douzhang8033 2014-03-28 12:50
    关注

    This topic should help you solving your problem.

    Adapted to your problem :

    $xml = simplexml_load_string($ch_result, NULL, NULL, "http://schemas.xmlsoap.org/soap/envelope/");
    $ns = $xml->getNamespaces(true);
    $soap = $xml->children($ns['env']);
    $res = $soap->Body->children($ns['ns2']);
    
    foreach ($res->LookupResponse as $item) {
        echo $item->Name.PHP_EOL;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看