How to Create Login web Service (API) in Wordpress

Hello Developers,
   if you want to make web service that send username or email and password in a url and find that login detail is correct or not and send user id related to that login detail with a json data. Your code look like that :

In your api folder (previously define in register api) create a file with login.php

Write below code :

<?php
require ("../wp-load.php");

if($_REQUEST['pass'] != "" && $_REQUEST['email'] != "") {

$ar = wp_authenticate_username_password("", $_REQUEST['email'], $_REQUEST['pass']);

$class_name = get_class ($ar);
if ( $class_name == "WP_User" ) {
$user_id = $ar->data->ID;

$arr = array(
"id" => $user_id,
"email" => $ar->data->user_email,
"name" => $ar->data->display_name,
);
$ar = array ("result" => 0, "message" => $arr);
echo json_encode($ar);
exit();
} else {
$ar = array ("result" => 0, "message" => "Email or Password is Wrong");
echo json_encode($ar);
exit();
}
} else {
$ar = array ("result" => 0, "message" => "Require Field Missing");
echo json_encode($ar);
exit();
}
?>

run your url with following link
www.webtecso.com/api/login.php?email=&pass=

Comments