url with parameters returns wrong value

0

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?

    
asked by anonymous 03.11.2016 / 23:43

2 answers

0

in the url should not contain spaces:

Solution:

   $this->cepDestino =  str_replace(" ","%20",$this->phpUtil->limpaCaracters($this->cepDestino));
    
04.11.2016 / 00:16
0

One suggestion would be to grab 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 on 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
    
04.11.2016 / 13:08