簡體   English   中英

在PHP中解析XML SOAP響應

[英]Parsing XML SOAP response in PHP

我一直試圖在PHP中解析XML SOAP響應,但是我仍然遇到錯誤。 我無法弄清楚為什么會發生這些錯誤。

來自服務器的$response ,以字符串形式存儲在$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/

任何幫助是極大的贊賞。

我不知道如何使用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";
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM