Reputation: 578
I'm trying to make a post call like this to return a jwt bearer token. But in php using cURL. So I can save the bearer token to a variable for use in the API calls.
$.ajax({
type: 'POST',
url: 'http://www.your-site.com/siteguard/api/prepare-public-jwt',
data: { api_key: 'YOUR_PUBLIC_API_KEY' },
dataType: 'json',
success: function (data) {
//Returns encoded JWT
console.log(data);
}
});
Php Code Im using
$url = $this->getTokenUrl();
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$headers = array(
"Content-Type: application/json",
);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
$data = ["api_key" => "xOMjVpz83rxDKjJUX9qNClB2BwadcRWjm09YSCdasdabdasdasdasdgTR8fuvR7jQHP8ZVpbOOmdXqKEt0AVX"];
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
//for debug only!
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$resp = curl_exec($curl);
curl_close($curl);
var_dump($resp);
The Return is this
{"code":401,"status":"error","response":"Invalid Public API key","link":"http:\/\/localhost\/siteguard\/api\/prepare-public-jwt"}"
Upvotes: 0
Views: 207
Reputation: 578
I was able to achieve it use this
$url = $this->getTokenUrl();
$curl = curl_init();
curl_setopt_array($curl, 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 => 'xOMjVpz83rxDKjJUX9qNClB2BwadcRWjm09YSCdasdabdasdasdasdgTR8fuvR7jQHP8ZVpbOOmdXqKEt0AVX',
CURLOPT_HTTPHEADER => array(
'Content-Type: application/x-www-form-urlencoded'
),
));
$response = curl_exec($curl);
$obj = json_decode($response);
curl_close($curl);
return $obj->response;
Upvotes: 1