XML only brings empty value

Asked

Viewed 75 times

1

I have the xml brought down from a cUrl.

<DistanceMatrixResponse>
<status>OK</status>
<origin_address>Muriaé - MG, 36880-000, Brasil</origin_address>
<destination_address>Patrocínio do Muriaé - MG, 36860-000, Brasil</destination_address>
<row>
<element>
<status>OK</status>
<duration>
<value>3939</value>
<text>1 hora 6 minutos</text>
</duration>
<distance>
<value>43371</value>
<text>43,4 km</text>
</distance>
</element>
</row>
</DistanceMatrixResponse>

I am trying in the following ways to read the status but it only gives empty.

When I give a print_r(), it reads all the xml on the screen.

 $frete = new CalculaFrete(36880000, 36860000);
 $freteXML1 = $frete->calculaFrete2();
 $freteXML2 = simplexml_load_file($frete->calculaFrete2());
 print "<pre>";
 print_r($freteXML1);
 print $freteXML1->row->element->status;
 print $freteXML2->row->element->status;
 print "</pre>";

Where am I going wrong?

  • The $freteXML1 returns the xml just above?

  • normal, right

1 answer

2


In that $xml I did the test and it worked, but it was used simplexml_load_string because a text with xml.

$xml = '<DistanceMatrixResponse>
<status>OK</status>
<origin_address>Muriaé - MG, 36880-000, Brasil</origin_address>
<destination_address>Patrocínio do Muriaé - MG, 36860-000, Brasil</destination_address>
<row>
<element>
<status>OK</status>
<duration>
<value>3939</value>
<text>1 hora 6 minutos</text>
</duration>
<distance>
<value>43371</value>
<text>43,4 km</text>
</distance>
</element>
</row>
</DistanceMatrixResponse>';

$obj = simplexml_load_string($xml);

print $obj->status;
print '<br />';
print $obj->row->element->status;

Example: IDEONE

  • 1

    that’s right, thank you!

  • Suddenly a question hit me: I’m searching the street in the base of the post office, and I’m searching the distance in the database of google maps using the same zip codes. But google maps also brings me the backyard too. Would it be reliable to rely on the database of google maps regarding the streets and do all the research there and leave the base of the post office aside? Or it wouldn’t be a good one?

  • @Carlosrocha I believe more in the database of the post office, now also depends on whether the base is updated...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.