Class yii\httpclient\XmlParser
Inheritance | yii\httpclient\XmlParser » yii\base\BaseObject |
---|---|
Implements | yii\httpclient\ParserInterface |
Available since extension's version | 2.0 |
Source Code | https://github.com/yiisoft/yii2-httpclient/blob/master/src/XmlParser.php |
XmlParser parses HTTP message content as XML.
Public Methods
Method | Description | Defined By |
---|---|---|
parse() | Parses given HTTP response instance. | yii\httpclient\XmlParser |
Protected Methods
Method | Description | Defined By |
---|---|---|
convertXmlToArray() | Converts XML document to array. | yii\httpclient\XmlParser |
Method Details
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;
}
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));
}