The JSON response
On calling every API request a JSON response will be issued with a HTTP status code. On the client side you have to verify the response http status code. If the status is 200, the request is processed successfully. Also you can notice a “error” node in the response. If the error value is true, that means some error occurred while processing the user data.
Api Calls without Authentication (without API key in the request header)
These calls don’t have to include Api Key in the request header. The main purpose of these calls is to interact with database without any authentication. User registration and login comes under this category.
⇒ User Registration
In order to interact with the API, the user has to register in our system first. Once he registered an API key will be generated and stored in the database. This API key will be private to that user only.
9. Add the following code in index.php. This function handles user registration.
index.php
/**
* User Registration
* url - /register
* method - POST
* params - name, email, password
*/
$app
->post(
'/register'
,
function
()
use
(
$app
) {
// check for required params
verifyRequiredParams(
array
(
'name'
,
'email'
,
'password'
));
$response
=
array
();
// reading post params
$name
=
$app
->request->post(
'name'
);
$email
=
$app
->request->post(
'email'
);
$password
=
$app
->request->post(
'password'
);
// validating email address
validateEmail(
$email
);
$db
=
new
DbHandler();
$res
=
$db
->createUser(
$name
,
$email
,
$password
);
if
(
$res
== USER_CREATED_SUCCESSFULLY) {
$response
[
"error"
] = false;
$response
[
"message"
] =
"You are successfully registered"
;
echoRespnse(201,
$response
);
}
else
if
(
$res
== USER_CREATE_FAILED) {
$response
[
"error"
] = true;
$response
[
"message"
] =
"Oops! An error occurred while registereing"
;
echoRespnse(200,
$response
);
}
else
if
(
$res
== USER_ALREADY_EXISTED) {
$response
[
"error"
] = true;
$response
[
"message"
] =
"Sorry, this email already existed"
;
echoRespnse(200,
$response
);
}
});
In the following table you can find the API request information about the URL, HTTP method and the parameters needed to be posted.
URL /register
Method POST
Params name, email, password
Upon the successful registration the following json response will be issued.
{
"error"
:
false
,
"message"
:
"You are successfully registered"
}
If the request is missing mandatory parameters the following json will be issued.
{
"error"
:
true
,
"message"
:
"Required field(s) email, password is missing or empty"
}
/** * User Registration * url - /register * method - POST * params - name, email, password */ $app ->post( '/register' , function () use ( $app ) { // check for required params verifyRequiredParams( array ( 'name' , 'email' , 'password' )); $response = array (); // reading post params $name = $app ->request->post( 'name' ); $email = $app ->request->post( 'email' ); $password = $app ->request->post( 'password' ); // validating email address validateEmail( $email ); $db = new DbHandler(); $res = $db ->createUser( $name , $email , $password ); if ( $res == USER_CREATED_SUCCESSFULLY) { $response [ "error" ] = false; $response [ "message" ] = "You are successfully registered" ; echoRespnse(201, $response ); } else if ( $res == USER_CREATE_FAILED) { $response [ "error" ] = true; $response [ "message" ] = "Oops! An error occurred while registereing" ; echoRespnse(200, $response ); } else if ( $res == USER_ALREADY_EXISTED) { $response [ "error" ] = true; $response [ "message" ] = "Sorry, this email already existed" ; echoRespnse(200, $response ); } }); |
URL | /register |
Method | POST |
Params | name, email, password |
{ "error" : false , "message" : "You are successfully registered" } |
{ "error" : true , "message" : "Required field(s) email, password is missing or empty" } |
0 comments:
Post a Comment