PHP - 解析SOAP响应中的数据 [英] PHP - parse data from a SOAP response

查看:474
本文介绍了PHP - 解析SOAP响应中的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用W3验证器API,我得到了这样的响应:

I'm using the W3 validator API, and I get this kind of response:

<?xml version="1.0" encoding="UTF-8"?>
<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope">
<env:Body>
<m:markupvalidationresponse env:encodingStyle="http://www.w3.org/2003/05/soap-encoding" xmlns:m="http://www.w3.org/2005/10/markup-validator">

    <m:uri>http://myurl.com/</m:uri>
    <m:checkedby>http://validator.w3.org/</m:checkedby>
    <m:doctype>-//W3C//DTD XHTML 1.1//EN</m:doctype>
    <m:charset>utf-8</m:charset>
    <m:validity>false</m:validity>
    <m:errors>
        <m:errorcount>1</m:errorcount>
        <m:errorlist>

            <m:error>
                <m:line>7</m:line>
                <m:col>80</m:col>
                <m:message>character data is not allowed here</m:message>
                <m:messageid>63</m:messageid>
                <m:explanation>  <![CDATA[
                 PAGE HTML IS HERE
                  ]]>
                </m:explanation>
                <m:source><![CDATA[ HTML AGAIN ]]></m:source>
            </m:error>

            ...

        </m:errorlist>
    </m:errors>
    <m:warnings>
        <m:warningcount>0</m:warningcount>
        <m:warninglist>


        </m:warninglist>
    </m:warnings>
</m:markupvalidationresponse>
</env:Body>
</env:Envelope>

如何提取一些变量?

我需要有效性错误计数,如果可能的话,从错误列表: col 消息:)

I need validity, errorcount and if possible from the list of errors: line, col, and message :)

有一个简单的方法来做到这一点吗?

Is there a easy way to do this?

推荐答案

$ c> SimpleXMLElement 与 simplexml_load_string ,然后使用XPath查找属性。在使用XPath之前注册涉及 registerXPathNamespace 的命名空间很重要。

You can load the XML string into a SimpleXMLElement with simplexml_load_string and then find the attributes using XPath. It's important to register the namespaces involved with registerXPathNamespace before using XPath.

$xml = file_get_contents('example.xml'); // $xml should be the XML source string
$doc = simplexml_load_string($xml);
$doc->registerXPathNamespace('m', 'http://www.w3.org/2005/10/markup-validator');
$nodes = $doc->xpath('//m:markupvalidationresponse/m:validity');
$validity = strval($nodes[0]);
echo 'is valid: ', $validity, "\n";
$nodes = $doc->xpath('//m:markupvalidationresponse/m:errors/m:errorcount');
$errorcount = strval($nodes[0]);
echo 'total errors: ', $errorcount, "\n";
$nodes = $doc->xpath('//m:markupvalidationresponse/m:errors/m:errorlist/m:error');
foreach ($nodes as $node) {
    $nodes = $node->xpath('m:line'); 
    $line = strval($nodes[0]);
    $nodes = $node->xpath('m:col');
    $col = strval($nodes[0]);
    $nodes = $node->xpath('m:message');
    $message = strval($nodes[0]);
    echo 'line: ', $line, ', column: ', $col, ' message: ', $message, "\n";
}

这篇关于PHP - 解析SOAP响应中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆