url with parameters returns wrong value

Asked

Viewed 54 times

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?

2 answers

0

One suggestion would be to take the data array and use the functions in ex:

$dados = array(
    'origins'=>'SP' 
    ,'language'=>'pt-br'
    ,'operators'=>array('1','2')
); 
// também é possivel com array dentro do array

$urlcoded = 'http://www.site.com/?'.http_build_query( $dados );

or if you prefer to do it in hand

$urlcoded = 'http://www.site.com/?';

foreach(dados as $k=>$v){
    $urlcoded .= $k.'='.urlencode($v).'&';
    // constroi formato key=valuecoded&key2=valuecoded2
}
$urlcoded = rtrim('$',$uelcoded);
// tira o ultimo & nao necessario

0

url must not contain spaces:

Solution:

   $this->cepDestino =  str_replace(" ","%20",$this->phpUtil->limpaCaracters($this->cepDestino));

Browser other questions tagged

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