simplexml_load_file() does not recognize urlldecode

Asked

Viewed 36 times

1

I have the following class for the calculation of distances:

<?php

class CalculaFrete {    

    # funcao de calculo da distãncia

    private function calculaDistancia(string $_cidade, string $_estado) : ?Array {

        $cidadeOrigem = 'Muriaé';
        $estadoOrigem = 'MG';

        $key = 'abcd...';

        $parametros = [
           'units'       =>'metric',
           'mode'        =>'driving',
           'origins'     => $cidadeOrigem . '-' . $estadoOrigem,
           'destinations'=> $_cidade . '-' . $_estado,
            'key'        => $key
        ];

        $urlDistancia = 'https://maps.googleapis.com/maps/api/distancematrix/json?' . urldecode( http_build_query ( $parametros ) ) ;       

        $xml = simplexml_load_file( utf8_encode( $urlDistancia ) );

        $distancia = json_decode($xml);

        return $distancia->rows->elements->status == 'OK' ? $distancia->rows->elements : null;

    }

    # funcao de calculo de preços e prazos para serviços de transportadoras

    public function calculaFreteTransportadora(string $_cidade, string $_estado, float $_precoKM, float $_quantidade) : ?float {

        $distancia = $this->calculaDistancia( $_cidade, $_estado);

        return is_null( $distancia ) ? null : $distancia * $_precoKM * $_quantidade;

    }


}

$frete = new CalculaFrete();

echo $frete->calculaFreteTransportadora('Carangola','MG', 0.56, 5) ;

and the response of

echo $frete->calculaFreteTransportadora('Carangola','MG', 0.56, 5) ;

is:

Warning: simplexml_load_file(): https://maps.googleapis.com/maps/api/distancematrix/json?units=metric&origins=Muria%C3%83%C2%A9-MG&destinations=Carangola-MG&mode=driving&key=abcd...:1: parser error : Start tag expected, '<' not found in D:\Trabalhos\host\htdocs\mvc_crud_pdo\classes\util\CalculaFrete.php on line 73

Obs.: abcd... is a substituting of key true for posting here on the forum.

Where am I going wrong?

it seems that the problem is in:

Muria%C3%83%C2%A9

when it comes to

simplexml_load_file

I have tried to agree with what the honourable Member said but the error persists:

$parametros = [
   'units'       =>'metric',
   'mode'        =>'driving',
   'origins'     => $cidadeOrigem . '-' . $estadoOrigem,
   'destinations'=> $_cidade . '-' . $_estado,
    'key'        => $key
];

$urlDistancia = 'https://maps.googleapis.com/maps/api/distancematrix/json?' . http_build_query ( $parametros ) ;        

$xml = simplexml_load_file( $urlDistancia );

$distancia = json_decode($xml);

and follows the error:

Warning: simplexml_load_file(): https://maps.googleapis.com/maps/api/distancematrix/json?units=metric&mode=driving&origins=Muria%C3%A9-MG&destinations=Carangola-MG&key=AIzaSyC8LfR2NO9E-5w8-sCPVH-uVLo9FRUz6jc:1: parser error : Start tag expected, '<' not found in D:\Trabalhos\host\htdocs\mvc_crud_pdo\classes\util\CalculaFrete.php on line 73
  • 1

    This is wrong urldecode( http_build_query ( $parametros ) ), or better is unnecessary. As I said in the other answer http_build_query does not need urlencode... Let alone utf8_encode --> utf8_encode( $urlDistancia ). Making these adjustments will probably fix the URL and it will work.

  • I made the adjustments and continued the error. I put the adjustment at the end of the question. Can I have one more look please? Thank you! Warning: simplexml_load_file(): https:/maps.googleapis.com/maps/api/distancematrix/json?units=metric&mode=driving&origins=Muria%C3%A9-Mg&destinations=Carangola-Mg&key=Aizasyc8lfr2no9e-5w8-sCPVH-uVLo9FRUz6jc:1: parser error : Start tag expected, '<' not found in D: Jobs host htdocs mvc_crud_pdo classes util Calculafrete.php on line 73

  • 1

    It is because this URL does not return an XML, it returns JSON. simplexml_load_file does not work with JSON, so far as I know. I recommend that you read the error ALWAYS, even if in English, after all your attempt to use urlencode and utf8_encode had no sense to solve this problem, you have to solve the problem requested by the language interpreter ;)

  • 1

    Trying to Read a Json file as XML. If you want to do this use Curl

No answers

Browser other questions tagged

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