How to authenticate Json with php by passing header

Asked

Viewed 1,120 times

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 example to perform such authentication.

http://docs.00k.io/v2/

URL BASE: All calls from this module must start from the following Base URL. http://api.00k.srv.br/

AUTHENTICATION: To access the API you need to pass two headers, are they:

Authorization: {String}
Content-Type: application/json;charset=UTF-8

1 answer

1


I don’t know this API, but the header I believe is like this:

 Authorization: Basic [login e senha em base64]

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

Authorization: [tipo] [credenciais]

In the doc says this:

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

So I guess the manager can tell you if it’s actually Basic, if it is basic and this using PHP can use so:

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

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

But that’s like I said, it’s just being the type Basic if it’s any other kind forget, that’s not how it’s gonna work.

Chance is really 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

Browser other questions tagged

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