yii2-httpclient icon indicating copy to clipboard operation
yii2-httpclient copied to clipboard

Can't get XML attribute from content

Open denis909 opened this issue 5 years ago • 3 comments

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;

denis909 avatar Feb 21 '20 12:02 denis909

Could you provide some code that could be used to reproduce it?

samdark avatar Feb 21 '20 12:02 samdark

$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);

denis909 avatar Feb 21 '20 12:02 denis909

php 7.3.2 Windows

denis909 avatar Feb 21 '20 12:02 denis909