PHP - parse data from a SOAP response

人盡茶涼 提交于 2019-12-01 06:56:54

问题


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>

How can I extract some variables from there?

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

Is there a easy way to do this?


回答1:


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



回答2:


You should be using a SOAP library to get this in the first place. There are various options you can try for this; nusoap, http://php.net/manual/en/book.soap.php, the zend framework also has SOAP client and server which you can use. Whatever implementation you use will allow you to get the data in some way. Doing a var_dump() on whatever holds the initial response should aid you in navigating through it.




回答3:


If you rather use the DOMDocument class from php. You don't have to know Xpath to get this working. An example:

$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() arguments are uneven because the empty text between tags is counted as an item.



来源:https://stackoverflow.com/questions/5048685/php-parse-data-from-a-soap-response

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!