I am trying to execute a PHP code base for GET API request, but I am getting
"AAR: 002" "No JWT Found"
error. below is the code
I am executing a GET request with cURL commands in PHP . The same code base and keys are working fine in Java and C# but when trying in PHP got this error.
When I try executing the below query
function getOpenSSLErrors()
{
$messages = [];
while ($msg = openssl_error_string()) {
$messages[] = $msg;
}
return $messages;
}
//function to generate JW token
function
generateJWT($algo,$header,$payload) {
$headerEncoded = str_replace(['+','/','='], ['-','_',''], base64_encode(json_encode($header)));
$payloadEncoded =str_replace(['+','/','='], ['-','_',''], base64_encode(json_encode($payload)));
// Delimit with period (.)
$dataEncoded ="$headerEncoded.$payloadEncoded";
$filename = "C:\\Jeyanthi\\xampp\\htdocs\\Jeyanthi_keys\\scbapibankingprivatekey.pem";
//$filename = "C:\\Ranjit\\xampp\\htdocs\\certs\\scb-api-banking-privatekey.pem";
$file1 = fopen($filename,"r");
$privateKey = fread($file1,filesize($filename));
//This will be your private key.- scb-api-banking-privatekey.pem
fclose($file1);
//echo $privateKey;
echo '************************************************************************************\n';
$privateKeyResource = openssl_pkey_get_private($privateKey,'123456');//password to fetch private key
$result = openssl_sign($dataEncoded,$signature,$privateKeyResource,$algo);
if ($result ===false)
{
throw new RuntimeException("Failed to generate signature: ".implode("\n",getOpenSSLErrors()));
}
$signatureEncoded =str_replace(['+','/','='], ['-','_',''],base64_encode($signature));
$jwt ="$dataEncoded.$signatureEncoded";
return $jwt;
}
// JWT Header
$header = ["alg" => "RS256","typ" => "JWT" ];
$iat = time();
$exp=$iat+30;
// JWT Payload data
$payload =['jti'=>'CLIENT','exp'=>$exp, 'iss'=>"CLIENT",
'aud'=>"SCB-APIBanking",'iat'=>$iat
];
// Create the JWT
$jwt = generateJWT('sha256',$header,$payload);
$sslCertPass = '123456'; //This will be the passphrase for p12 certificate
//$sslCert='client.cer';
$filenameCert = "C:\\Jeyanthi\\xampp\\htdocs\\Jeyanthi_keys\\client.pem";
//$filenameCert = "C:\\Ranjit\\xampp\\htdocs\\certs\\client.pem";
$sslCert = fread( fopen($filenameCert,"r"),filesize($filenameCert));
//This will be your private key.- scb-api-banking-privatekey.pem
//This will be the client.cer provided as part of activation - CURLOPT_SSLCERT
//echo $sslCert;
//$sslKey = 'scb-apibanking-client-cert-private-key.pem'; //This will be your private key.pem
$privatekey_sslkey=realpath("C:\\Jeyanthi\\xampp\\htdocs\\Jeyanthi_keys\\clientcertsslprivatekey.pem");
//$privatekey_sslkey=realpath("C:\\Ranjit\\xampp\\htdocs\\certs\\scb-apibanking-client-cert-key.pem");
if(!$privatekey_sslkey || !is_readable($privatekey_sslkey)){
die("error: client.pem is not readable! realpath:\"{$privatekey_sslkey}\"- working dir: \"".getcwd()."\"effective user: ".print_r(posix_getpwuid(posix_geteuid()),true));
}
// $url = "https://apitest.standardchartered.com/activate";
//$url = "https://apitest.standardchartered.com/uat2/openapi/subscriptions/v2/credit-debit-advice";
$url = "https://apitest.standardchartered.com/uat2/openapi/subscriptions/v2";
//Post the activation token
$ch = curl_init($url );
$variable = "sample";
$pem=realpath("C:\\Jeyanthi\\xampp\\htdocs\\Jeyanthi_keys\\client.pem");
if(!$pem || !is_readable($pem)){
die("error: client.pem is not readable! realpath:\"{$pem}\" - working dir: \"".getcwd()."\"effective user: ".print_r(posix_getpwuid(posix_geteuid()),true));
}//echo $jwt ;
$options = array(
//CURLOPT_FAILONERROR => false,
CURLOPT_RETURNTRANSFER => true,
// CURLOPT_AUTOREFERER => true,
// CURLOPT_HEADER => true,
// CURLOPT_NOBODY =>true,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_SSLKEY =>$privatekey_sslkey,
CURLOPT_SSLCERT => $pem,
CURLOPT_KEYPASSWD => $sslCertPass,
CURLOPT_SSLCERTPASSWD =>$sslCertPass,
CURLOPT_SSL_VERIFYHOST => 2,
CURLOPT_SSL_VERIFYPEER => true,
// CURLOPT_POST => false,
// CURLOPT_POSTFIELDS => $jwt,
CURLOPT_HTTPHEADER => array(
"Content-Type => application/json",
"JWTToken => $jwt",
"Pragma => akamai-x-get-extracted-values"
),
);
//print_r( $options);
curl_setopt_array($ch,$options );
$resp = curl_exec($ch);
echo $resp;
$ch_errno = curl_errno($ch);
$ch_erro = curl_error($ch);
curl_close($ch);
if ($ch_errno)
{
echo "cURL Error #:". $ch_erro;
}
else
{
echo "Response:".$resp;
}
?>