Author Image

lorenacastro

Nov 21, 2020

Autenticação com cURL e Certificados

APIAutenticaçãoautorizaçãoCurlintegração

function auth() {
$file = file_get_contents("./config.json");
$config = json_decode($file, true);
$environment = ($config["sandbox"] === true) ? "development" : "production";

$certfile = $config[$environment]["certificate_name"];

$curl = curl_init();

$authorization = base64_encode($config[$environment]["client_id"] . ":" . $config[$environment]["client_secret"]);

curl_setopt_array($curl, array(
CURLOPT_URL => $config[$environment]["pix_auth_url"], // Rota base, desenvolvimento ou produção
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => '{"grant_type":"client_credentials"}',
CURLOPT_SSLCERT => $certfile, // Caminho do certificado
CURLOPT_SSLCERTPASSWD => "",
CURLOPT_HTTPHEADER => array(
"Authorization: Basic $authorization",
"Content-Type: application/json"
),
));

$response = curl_exec($curl);

curl_close($curl);

echo "

";
echo $response;
echo "
";
}

Respostas (1):
Avatar discord do usuario lorenacastro

lorenacastro

21/11/2020

esse