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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀