Hello, I got it working as you can see in the following image:
For this I used the following code:
<?php
//Get Token
function getToken($user,$pass){
$url = 'http://localhost:5400/api/oauth/token';
$ch = curl_init($url);
$data = array(
'grant_type' => 'password',
'username' => $user,
'password' => $pass
);
$payload = json_encode($data);
curl_setopt($ch, CURLOPT_POSTFIELDS,$payload);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type:application/json'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
$var = json_decode($result,true);
$token=$var['access_token'];
curl_close($ch);
return $token;
}
//Submit the globalMessage with the token
function globalMessage($message,$token){
$url = 'http://localhost:5400/api/v1/chat/global';
$ch = curl_init($url);
$data = array(
'Message' => $message
);
$payload = json_encode($data);
curl_setopt($ch, CURLOPT_POSTFIELDS,$payload);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type:application/json','Authorization: Bearer '.$token));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
$status = curl_getinfo($ch,CURLINFO_HTTP_CODE);
curl_close($ch);
return $status;
}
?>
Then you can just send the request through:
$token=getToken($user,$pass);
echo $test = globalMessage("Submit global message through the API using PHP",$token);
You should get as a response 200 since it is a success