I have the following query with api
of Google Maps
http://maps.googleapis.com/maps/api/distancematrix/xml?origins=Muriae&destinations=Rio de Janeiro&mode=CAR&language=pt-br&sensor=false
That returns me the following array
<DistanceMatrixResponse>
<status>OK</status>
<origin_address>Muriaé, MG, Brasil</origin_address>
<destination_address>Rio de Janeiro, RJ, Brasil</destination_address>
<row>
<element>
<status>OK</status>
<duration>
<value>15350</value>
<text>4 horas 16 minutos</text>
</duration>
<distance>
<value>298184</value>
<text>298 km</text>
</distance>
</element>
</row>
</DistanceMatrixResponse>
So I'm doing the following função
to pegar
o value
of distancia
.
private function calculaDistancia () {
//$url = "http://maps.googleapis.com/maps/api/distancematrix/xml?origins=".$this->cepOrigem."&destinations=".$this->cepDestino."&mode=".$this->mode."&language=".$this->language."&sensor=false";
$url = "http://maps.googleapis.com/maps/api/distancematrix/xml?origins=Muria%C3%A9&destinations=Rio%20de%20Janeiro&mode=CAR&language=pt-br&sensor=false";
print $url;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$data = curl_exec($ch);
$freteXML = simplexml_load_string($data);
$distancia = $freteXML->row->element->distance->value; // CalculaFrete.php on line 60
return $distancia;
}
But it is giving error when using variáveis
in the function.
Notice: Trying to get property of non-object in C:\Program Files (x86)\Apache Software Foundation\Apache2.2\htdocs\php\hotplateprensas.com.br\_controlls\_util\CalculaFrete.php on line 60 Notice: Trying to get property of non-object in C:\Program Files (x86)\Apache Software Foundation\Apache2.2\htdocs\php\hotplateprensas.com.br\_controlls\_util\CalculaFrete.php on line 60 Notice: Trying to get property of non-object in C:\Program Files (x86)\Apache Software Foundation\Apache2.2\htdocs\php\hotplateprensas.com.br\_controlls\_util\CalculaFrete.php on line 60 Notice: Trying to get property of non-object in C:\Program Files (x86)\Apache Software Foundation\Apache2.2\htdocs\php\hotplateprensas.com.br\_controlls\_util\CalculaFrete.php on line 60
Direct return browser but with curl () will not.
Another thing, giving print
, url
prints
http://maps.googleapis.com/maps/api/distancematrix/xml?origins=Muria%C3%A9&destinations=Rio%20de%20Janeiro&mode=CAR&language=pt-br&sensor=false
If I, instead of using the%% variables, use php
directly in the function, also works.
Where am I going wrong?