<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);

$url = 'https://idp.comprobanteselectronicos.go.cr/auth/realms/rut-stag/protocol/openid-connect/token';//access token url
 	
$data = array(
	'client_id' => "api-stag",
	'client_secret' => "",
	'grant_type' => "password",
	'username' => "cpf-01-1493-0394@stag.comprobanteselectronicos.go.cr", 
	'password' => ")*|#%tmwwz}+I|B5-^zl", 
	'scope' =>""     
);

$options = array(
	'http' => array(
		'header'  => "Content-type: application/x-www-form-urlencoded\r\n",
		'method'  => 'POST',
		'content' => http_build_query($data)
		 )			 
	);
	
$context  = stream_context_create($options);
$resultadoToken = file_get_contents($url, false, $context);
$token = json_decode($resultadoToken);
if(isset($_GET['debug']))
	echo $token->access_token;