How to authenticate Json with php by passing header

0

I have the following documentation, I would like to know how to do the authentication, and if possible where I can get study material that has an example to perform such authentication.

link

BASE URL: All calls from this module must start from the following Base URL. link

AUTHENTICATION: To access the API it is necessary to pass two headers, they are:

Authorization: {String}
Content-Type: application/json;charset=UTF-8
    
asked by anonymous 21.09.2017 / 19:31

1 answer

1

I do not know this API, but the header I think it's like this:

 Authorization: Basic [login e senha em base64]

If is of type Basic , to summarize it works like this:

Authorization: [tipo] [credenciais]

In the doc say this:

  

Each store has its own authentication key to be used in the HTTP Authorization header, which should be requested from your account manager at 00K.

So I think the manager can tell you if it's actually Basic , if it's basic and using PHP you can use it like this:

$usuario = 'usuario';
$senha = 'senha';

$header = 'Authorization: Basic ' . base64_encode($usuario . ':' . $senha);

But this is what I said, it's just to be of type Basic if it's another type, forget it, that's not how it works.

Maybe it really is Basic follow some examples below:

Using Basic with file_get_contents

$usuario = 'usuario';
$senha = 'senha';

$url = 'http://api.00k.srv.br/';

$header = 'Authorization: Basic ' . base64_encode($usuario . ':' . $senha) . "\r\n"
          'Content-Type: application/json;charset=UTF-8';

$context = stream_context_create(array(
    'http' => array(
                  'header' => $header
              )
    )
);
$homepage = file_get_contents($url, false, $context);

var_dump($data); //Visualiza a resposta

Using Basic with curl

$usuario = 'usuario';
$senha = 'senha';

$url = 'http://api.00k.srv.br/';

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);

curl_setopt($ch, CURLOPT_HTTPHEADER, array(
   'Content-Type: application/json;charset=UTF-8'
));

curl_setopt($ch, CURLOPT_USERPWD, $username . ':' . $password);

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);

$data = curl_exec($ch);

if($data === false) {
    echo 'Erro ao executar o CURL: ' . curl_error($ch);
} else {
    $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

    if ($httpcode !== 200) {
        echo 'Erro ao requisitar o servidor';
    }
}

curl_close($ch);

var_dump($data); //Visualiza a resposta
    
21.09.2017 / 21:03