在PHP中解析XML SOAP响应

时间:2017-12-20 18:45:06

标签: php xml soap

我一直在尝试用PHP解析XML SOAP响应,但我仍然会遇到错误。我无法弄清楚为什么会出现这些错误。

来自服务器的响应,作为字符串存储在$response中(删除了敏感数据):

<?xml version="1.0" encoding="utf-8"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
    <s:Header>
        <h:ServerVersionInfo MajorVersion="15" MinorVersion="20" MajorBuildNumber="323" MinorBuildNumber="19" Version="V2017_10_09" xmlns:h="http://schemas.microsoft.com/exchange/services/2006/types" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
    </s:Header>
    <s:Body>
        <m:GetAttachmentResponse xmlns:m="http://schemas.microsoft.com/exchange/services/2006/messages" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types">
            <m:ResponseMessages>
                <m:GetAttachmentResponseMessage ResponseClass="Success">
                    <m:ResponseCode>NoError</m:ResponseCode>
                    <m:Attachments>
                        <t:FileAttachment>
                            <t:AttachmentId Id="id number"/>
                            <t:Name>message-footer.txt</t:Name>
                            <t:ContentType>text/plain</t:ContentType>
                            <t:ContentId>contentid.prod.outlook.com</t:ContentId>
                            <t:Content>file contents</t:Content>
                        </t:FileAttachment>
                    </m:Attachments>
                </m:GetAttachmentResponseMessage>
            </m:ResponseMessages>
        </m:GetAttachmentResponse>
    </s:Body>
</s:Envelope>

我的代码:

$data = simplexml_load_string($response);
$fileData = $data
    ->children('s:', true)->Body
    ->children('m:', true)->GetAttachmentResponse->ResponseMessages->GetAttachmentResponseMessage->Attachments
    ->children('t:', true)->FileAttachment;

我需要能够获取文件名,内容类型和内容。我继续收到以下错误:Node no longer exists(此处第4行)。

作为参考,我一直在关注本指南:https://joshtronic.com/2014/07/13/parsing-soap-responses-with-simplexml/

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:2)

我不知道如何使用simplexml阅读如此复杂的XML文件,但我知道DOMDocument非常适合它。

<?php

$source = file_get_contents('file.xml');

$dom = new DOMDocument("1.0", "UTF-8");
$dom->preserveWhiteSpace = false;
$dom->loadXml($source);
$xpath = new DOMXPath($dom);
$xpath->registerNamespace("s", "http://schemas.xmlsoap.org/soap/envelope/");
$xpath->registerNamespace("m", "http://schemas.microsoft.com/exchange/services/2006/messages");
$xpath->registerNamespace("xsd", "http://www.w3.org/2001/XMLSchema");
$xpath->registerNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
$xpath->registerNamespace("t", "http://schemas.microsoft.com/exchange/services/2006/types");

$fileAttachments = $xpath->query('//m:GetAttachmentResponseMessage/m:Attachments/t:FileAttachment');

/* @var DOMElement $fileAttachment */
foreach ($fileAttachments as $fileAttachment) {
    echo 'Name: ' . $xpath->query('t:Name', $fileAttachment)->item(0)->nodeValue . "\n";
    echo 'ContentType: ' . $xpath->query('t:ContentType', $fileAttachment)->item(0)->nodeValue  . "\n";
    echo 'Content: ' . $xpath->query('t:Content', $fileAttachment)->item(0)->nodeValue  . "\n";
}