Class yii\httpclient\XmlParser

Inheritanceyii\httpclient\XmlParser » yii\base\BaseObject
Implementsyii\httpclient\ParserInterface
Available since extension's version2.0
Source Code https://github.com/yiisoft/yii2-httpclient/blob/master/src/XmlParser.php

XmlParser parses HTTP message content as XML.

Public Methods

Hide inherited methods

Method Description Defined By
parse() Parses given HTTP response instance. yii\httpclient\XmlParser

Protected Methods

Hide inherited methods

Method Description Defined By
convertXmlToArray() Converts XML document to array. yii\httpclient\XmlParser

Method Details

Hide inherited methods

convertXmlToArray() protected method

Converts XML document to array.

protected array convertXmlToArray ( $xml )
$xml string|SimpleXMLElement

Xml to process.

return array

XML array representation.

                protected function convertXmlToArray($xml)
{
    if (is_string($xml)) {
        $xml = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
    }
    $result = (array) $xml;
    foreach ($result as $key => $value) {
        if (!is_scalar($value)) {
            $result[$key] = $this->convertXmlToArray($value);
        }
    }
    return $result;
}

            
parse() public method

Parses given HTTP response instance.

public mixed parse ( yii\httpclient\Response $response )
$response yii\httpclient\Response

HTTP response instance.

return mixed

Parsed content data.

                public function parse(Response $response)
{
    $contentType = $response->getHeaders()->get('content-type', '');
    if (preg_match('/charset=(.*)/i', $contentType, $matches)) {
        $encoding = $matches[1];
    } else {
        $encoding = 'UTF-8';
    }
    $dom = new \DOMDocument('1.0', $encoding);
    $dom->loadXML($response->getContent(), LIBXML_NOCDATA);
    return $this->convertXmlToArray(simplexml_import_dom($dom->documentElement));
}