0
I have the following appointment to 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 here’s what I’m doing função
for pegar
the 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’s making a mistake when I use variáveis
in 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
directly in the return browser but with Curl() will not.
Another thing, giving print
, to 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 lifts php
use the url
directly in the function, also works.
Where am I going wrong?