I
I
Ilya Beloborodov2015-10-29 13:18:52
PHP
Ilya Beloborodov, 2015-10-29 13:18:52

How to get a property from this object?

I work with Yandex Metrica.
I get this answer

<?xml version="1.0" encoding="UTF-8"?>

<doc xmlns="http://api.yandex.ru/metrika/">
  <id>-------------</id>
  <date1>20151023</date1>
  <date2>20151029</date2>
  <data count="3">
    <row>
      <exit>0</exit>
      <page_views>1</page_views>
      <id>508974949722839696</id>
      <entrance>0</entrance>
      <url><![CDATA[http://example.com/1]]></url>
    </row>
    <row>
      <exit>1</exit>
      <page_views>1</page_views>
      <id>2290409950598088779</id>
      <entrance>0</entrance>
      <url><![CDATA[http://example.com/2]]></url>
    </row>
    <row>
      <exit>0</exit>
      <page_views>1</page_views>
      <id>16120082494497546606</id>
      <entrance>1</entrance>
      <url><![CDATA[http://example.com/3]]></url>
    </row>
  </data>
  <totals>
    <exit>1</exit>
    <page_views>3</page_views>
    <entrance>1</entrance>
  </totals>
</doc>

with the help simplexml_load_string()I load the whole thing into an object
object(SimpleXMLElement)#31 (5) {
  ["id"]=>
  string(8) "----------"
  ["date1"]=>
  string(8) "20151023"
  ["date2"]=>
  string(8) "20151029"
  ["data"]=>
  object(SimpleXMLElement)#32 (2) {
    ["@attributes"]=>
    array(1) {
      ["count"]=>
      string(1) "3"
    }
    ["row"]=>
    array(3) {
      [0]=>
      object(SimpleXMLElement)#34 (5) {
        ["exit"]=>
        string(1) "0"
        ["page_views"]=>
        string(1) "1"
        ["id"]=>
        string(18) "508974949722839696"
        ["entrance"]=>
        string(1) "0"
        ["url"]=>
        object(SimpleXMLElement)#37 (0) {
        }
      }
      [1]=>
      object(SimpleXMLElement)#35 (5) {
        ["exit"]=>
        string(1) "1"
        ["page_views"]=>
        string(1) "1"
        ["id"]=>
        string(19) "2290409950598088779"
        ["entrance"]=>
        string(1) "0"
        ["url"]=>
        object(SimpleXMLElement)#37 (0) {
        }
      }
      [2]=>
      object(SimpleXMLElement)#36 (5) {
        ["exit"]=>
        string(1) "0"
        ["page_views"]=>
        string(1) "1"
        ["id"]=>
        string(20) "16120082494497546606"
        ["entrance"]=>
        string(1) "1"
        ["url"]=>
        object(SimpleXMLElement)#37 (0) {
        }
      }
    }
  }
  ["totals"]=>
  object(SimpleXMLElement)#33 (3) {
    ["exit"]=>
    string(1) "1"
    ["page_views"]=>
    string(1) "3"
    ["entrance"]=>
    string(1) "1"
  }
}

How to get the url property ?
if I do it like this, $xml->data->row[0]->url
I get this:
object(SimpleXMLElement)#34 (1) {
  [0]=>
  object(SimpleXMLElement)#32 (0) {
  }
}

if I do it like this, $xml->data->row[0]->url[0]
I get this:
object(SimpleXMLElement)#32 (0) {
}

Tell me what's wrong?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Zelensky, 2015-10-29
@kowap

you can see in the dump that the URL is empty, you call it correctly, the parser does not correctly parse the url value I
read a little, CDATA can be parsed through the DOM Documentation , google simpleXML DOM

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question