Play variables to be parameters in the API

Asked

Viewed 153 times

0

I have a website and to enter it you need to log in, and the data are: Cpf and email. And I have an API that I need to play the variables Cpf and email to be parameters in the api, but I don’t have much knowledge in api and also just have the link to it.

I need to play this data because each user will have different data, and the api will need this data in the parameter to select and fetch data from the person registered with Cpf and email.

ex.: http://api/api/TRCKCLICODCPF=12345678901&EMAIL=fulano%40email.com

Consuming API:

 function load(){

  var xhr = new XMLHttpRequest();
  xhr.open("GET", "http://api/api/TRCKCLICODCPF=12345678901&EMAIL=fulano%40email.com");



  xhr.addEventListener("load", function() {
      var resposta = xhr.responseText;
      // console.log("ola1");
      var clientes = JSON.parse(resposta);
      // console.log("ola2");
      // console.log(clientes);

      for (var i =0; i < 1; i++){
          // console.log("ola3");
         var clientes_1 = clientes.TRACKER[i];
         AdicionaNome(clientes_1);
         AdicionaCPF(clientes_1);
         AdicionaProduto(clientes_1);
         AdicionaCidade(clientes_1);
         AdicionaCodigoProduto(clientes_1);
         AdicionaCodigoCliente(clientes_1);
         AdicionaStatus(clientes_1);
         ActiveStatusImage(clientes_1);
         ActiveOnlyPostagem(clientes_1);
         adicionaClienteNaTabelaViagem(clientes_1);
         ActiveQtdViagem(clientes_1);


     }

  });

  xhr.send();
      }
      window.onload = load;

I changed the api link, it’s just an example

1 answer

0

An interesting method is to access via Curl, is similar to an ajax request, but with PHP, also check if the API has any comprehensive documentation...

Usually for authentication the method is GET, so it would be something like this...

PHP

$url="http://api/api/TRCKCLICODCPF=12345678901&EMAIL=fulano%40email.com";
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);    // get the url contents

$data = curl_exec($ch); // execute curl request
curl_close($ch);

$xml = simplexml_load_string($data);
print_r($xml)

So your API will not be visible to any user if accessed from the client...

  • And I play this code where?

  • Create the PHP file of the code, and access as you want, I recommend to access by ajax, it will return you the return of the API, it is logical that you will have to adapt it to your application. If you don’t understand the code above, here on the site and in others you have everything you need about accessing functions, Url and ajax, just search...

  • Ah, I did that. I asked the question, you can look and see if that’s what you’re talking about?

  • You’re consuming the API in the client, I never did that so I can’t tell you what to do, but from what I saw is based on XML, Curl also has XML support, I’ll edit with a simpler example.

  • is local, I do not have access, in fact never expose a private API link here on the site :)

  • I modified her kkk was just an example

Show 1 more comment

Browser other questions tagged

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