Authenticate a Json API with PHP

1

I need to do a user authentication in a JSON format API, I have all the documentation for this API. After you authenticate I can make the queries I need.

Developer says that I treasure this to authenticate.

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

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

Authorization: {Eu ja tenho a STRING}

What do I need to do to log in to php? Where can I find material on how to do it?

    
asked by anonymous 18.03.2017 / 01:40

1 answer

3

If you only need these two headers, use cURL% w / o. ;)

That way it would look like:

curl -H "Authorization: {String}" -H "Content-Type: application/json;charset=UTF-8" https://site-alvo.com

In PHP, however, it would look like this:

// Inicia o CURL, definindo o site alvo:
$ch = curl_init('https://site-alvo.com');

// Adiciona as opções:
curl_setopt_array($ch, [

      // Equivalente ao -H:
      CURLOPT_HTTPHEADER => [
           'Authorization: {String}',
           'Content-Type: application/json;charset=UTF-8',
      ],

      // Permite obter resposta:
      CURLOPT_RETURNTRANSFER => true

]);

// Executa:
$resultado = curl_exec($ch);

// Encerra CURL:
curl_close($ch);

In this way get the answer using:

var_dump($resultado);

However possibly there is an error in the question itself. The -H has the default Authorization , this was determined by the W3C .

For example, in the case of HTTP Auth Basic, default:

Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==

This is Authorization: <type> <credentials> Authorization: + Basic .

In other cases, defaults to BASE64(Login:Senha) , which comes from OAuth2, here you have the details of it .

Personally I do not know a type of:

Authorization: JSON {}

Possibly the previous call you make returns a JSON as follows:

{
  "access_token":"mF_9.B5f-4.1JqM",
  "token_type":"Bearer",
  "expires_in":3600,
  "refresh_token":"tGzv3JOkF0XG5Qx2TlKWIA"
}

This is exactly the example of specified here , so you should use Bearer Token in :

Authorization: Bearer mF_9.B5f-4.1JqM

But without specific documentation it is impossible to guess!

    
18.03.2017 / 04:25