PHP - 解析SOAP响应中的数据

时间:2011-02-19 03:37:11

标签: php soap curl w3c-validation

我正在使用W3验证器API,我得到了这种响应:

<?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>

如何从那里提取一些变量?

我需要validityerrorcount,如果可能,还需要错误列表:linecolmessage:)

有一种简单的方法吗?

3 个答案:

答案 0 :(得分:5)

您可以将XML字符串加载到SimpleXMLElement simplexml_load_string,然后使用XPath查找属性。在使用XPath之前注册registerXPathNamespace涉及的名称空间非常重要。

$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";
}

答案 1 :(得分:3)

您应该首先使用SOAP库来实现此目的。你可以尝试各种各样的选择; nusoap,http://php.net/manual/en/book.soap.php,zend框架也有SOAP客户端和服务器,你可以使用它。无论您使用什么实现,都可以以某种方式获取数据。对任何保留初始响应的内容执行var_dump()应该可以帮助您浏览它。

答案 2 :(得分:3)

如果您更喜欢使用DOMDocument class from php。您无需了解Xpath即可实现此功能。一个例子:

$url = "http://www.google.com";
$xml = new DOMDocument();
$xml->load("http://validator.w3.org/check?uri=".urlencode($url)."&output=soap12"); 

$doctype = $xml->getElementsByTagNameNS('http://www.w3.org/2005/10/markup-validator', 'doctype')->item(0)->nodeValue;
$valid = $xml->getElementsByTagNameNS('http://www.w3.org/2005/10/markup-validator', 'validity')->item(0)->nodeValue;
$errorcount = $xml->getElementsByTagNameNS('http://www.w3.org/2005/10/markup-validator', 'errorcount')->item(0)->nodeValue;
$warningcount = $xml->getElementsByTagNameNS('http://www.w3.org/2005/10/markup-validator', 'warningcount')->item(0)->nodeValue;

$errors = $xml->getElementsByTagNameNS('http://www.w3.org/2005/10/markup-validator', 'error');
foreach ($errors as $error) {
    echo "<br>line: ".$error->childNodes->item(1)->nodeValue;
    echo "<br>col: ".$error->childNodes->item(3)->nodeValue;
    echo "<br>message: ".$error->childNodes->item(5)->nodeValue;
}

// item()参数不均匀,因为标记之间的空文本被计为项目。