So I created a wrapper class to authenticate user via POST HTTP request.
class Nucleolus
{
public const AUTHEN_USER_URL = "localhost:8000/authen/user";
protected $ch;
public function __construct(string $token)
{
$this->ch = curl_init();
curl_setopt($this->ch, CURLOPT_URL, self::AUTHEN_USER_URL);
curl_setopt($this->ch, CURLOPT_HTTPHEADER, [
"Content-Type: application/json",
"Authorization: Bearer " . $token,
]);
curl_setopt($this->ch, CURLOPT_POST, 1);
curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, true);
}
public function authenticate(string $username, string $password, string $serverUrl = null)
{
$url = $serverUrl ? $serverUrl : $_SERVER['SERVER_NAME'];
curl_setopt($this->ch, CURLOPT_POSTFIELDS, json_encode(compact('url', 'username', 'password')));
return json_decode(curl_exec($this->ch), true);
}
public function __destruct()
{
curl_close($this->ch);
}
}
Then the other developers can do the following.
$nucleolus = new Nucleolus($token);
$result = $nucleolus->authenticate($username, $password);
and retrieve result as a PHP array.
Please review.