yii2-httpclient
yii2-httpclient copied to clipboard
Can't get XML attribute from content
I have this result from server:
<?xml version="1.0" encoding="UTF-8"?>
<yandexsearch version="1.0"><response date="20200221T142812"><error code="15">Для заданного поискового запроса отсутствуют результаты поиска.</error></response></yandexsearch>
$response->data return this array:
array(2) {
["@attributes"]=>
array(1) {
["version"]=>
string(3) "1.0"
}
["response"]=>
array(2) {
["@attributes"]=>
array(1) {
["date"]=>
string(15) "20200221T150244"
}
["error"]=>
string(119) "Для заданного поискового запроса отсутствуют результаты поиска."
}
}
code="15" is not in data array. I can get this code manually, but I think it would be better if it is in @attributes in data.
$xml = simplexml_load_string($response->content, 'SimpleXMLElement', LIBXML_NOCDATA);
$code = (string) $xml->response->error->attributes()->code;
Could you provide some code that could be used to reproduce it?
$client = new \yii\httpclient\Client;
$response = new \yii\httpclient\Response(['client' => $client]);
$response->setContent('<?xml version="1.0" encoding="UTF-8"?>
<yandexsearch version="1.0"><response date="20200221T142812"><error code="15">Для заданного поискового запроса отсутствуют результаты поиска.</error></response></yandexsearch>');
var_dump($response->data);
php 7.3.2 Windows