1. Create a base API superclass
class API
{
protected $response = array();
public function __construct()
{
$this->contentType('application/json');
}
protected function contentType($type)
{
header("Content-Type: $type");
}
protected function responseJson($statusCode, $statusMessage, $data)
{
header("HTTP/1.1 $statusCode $statusMessage");
$this->response['statusCode'] = $statusCode;
$this->response['statusMessage'] = $statusMessage;
$this->response['data'] = $data;
return json_encode($this->response);
}
}
2. Create a subclass that extends above API
class PasswordServiceAPI extends API
{
/**
* Hash the plain password provided
*
* http://legacyapp.cpom/api/PasswordServiceAPI/hashPassword?plain=Ab1L853Z24N
*
* @return json
*/
public function hashPassword()
{
if (!isset($_GET['plain']) or empty($_GET['plain'])) {
echo $this->responseJson(
422,
"Unprocessable Entity",
null
);
return;
}
$hash = bcrypt($_GET['plain']);
echo $this->responseJson(
200,
"Success",
$hash
);
}
}