FAQ Auditto

login backend

$username,
‘password’ => $password
);

// Tentando fazer o login
$login_url = ‘http://localhost:8080/wordpress/wp-json/api/v1/mo-jwt’; //trocar https://faq-temp.auditto.com.br/wp-json/api/v1/mo-jwt
$login_result = make_curl_request($login_url, $login_data, array(‘Content-Type: application/x-www-form-urlencoded’));
$login_response = $login_result[‘response’];
$login_httpcode = $login_result[‘httpcode’];

if ($login_httpcode === 200 && isset($login_response[‘jwt_token’])) {
// Sucesso, redirecionar com o token na URL
$jwt_token = $login_response[‘jwt_token’];
header(“Location: http://localhost:8080/wordpress/?mo_jwt_token={$login_response[‘jwt_token’]}”); //trocar https://faq-temp.auditto.com.br?mo_jwt_token=
exit();
} else {
// Login falhou, tentar registrar o usuário
$register_data = array(
‘username’ => $username,
‘password’ => $password,
‘apikey’ => $api_key
);

$register_url = ‘http://localhost:8080/wordpress/wp-json/api/v1/mo-jwt-register’; //trocar https://faq-temp.auditto.com.br/wp-json/api/v1/mo-jwt-register
$register_result = make_curl_request($register_url, $register_data, array(‘Content-Type: application/x-www-form-urlencoded’));
$register_response = $register_result[‘response’];
$register_httpcode = $register_result[‘httpcode’];

if ($register_httpcode === 200 && isset($register_response[‘jwt_token’])) {
// Sucesso, redirecionar com o token na URL
$jwt_token = $register_response[‘jwt_token’];
echo ““;
header(“Location: http://localhost:8080/wordpress/?mo_jwt_token={$register_response[‘jwt_token’]}”); //trocar https://faq-temp.auditto.com.br?mo_jwt_token=
exit();
} else {
//erro
echo ‘HTTP Code:’ . $register_httpcode . ‘ – Mensagem: ‘ . $register_response[‘message’];
echo ‘

';
            print_r($register_result['raw_response']); 
            echo '

‘;
}
}
}
// Realizar a requisição cURL
function make_curl_request($url, $data, $headers = array()) {
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => $url,
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 => http_build_query($data),
CURLOPT_HTTPHEADER => $headers,
));
$response = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return array(‘response’ => json_decode($response, true), ‘httpcode’ => $httpcode, ‘raw_response’ => $response);
}
?>