Work

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 2

<?

php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Response;
use Validator;
use App\Models\AppUser;
use Hash;

class APIController extends Controller


{
function createUser(Request $req){
$validation = Validator::make($req->all(),[
'name'=>'required|min:3|max:50',
'phone'=>['required','min:10','max:10', 'regex:/^[0-9]{10}$/'],
'email'=>'required|email|unique:app_users,email',
'password'=>'required|min:8|confirmed',
]);
if ($validation->fails())
{
return Response::json(['code'=>406,'errors'=>$validation->errors()]);
}
$u = new AppUser();
$u->name = $req->post('name');
$u->phone = $req->post('phone');
$u->email = $req->post('email');
$u->password = Hash::make($req->post('password'));
$u->save();
return Response::json(['code'=>200,'message'=>'Successfully User
Created']);
}
function updateUser(Request $req){
$validation = Validator::make($req->all(),[
'id'=>'required',
]);
if ($validation->fails())
{
return Response::json(['code'=>406,'errors'=>$validation->errors()]);
}
$id = $req->post('id');
$user = AppUser::find($id);
if(empty($user))
return Response::json(['code'=>404,'message'=>"User Not Found"]);
if(!empty($req->post('name')))
$user->name = $req->post('name');
if(!empty($req->post('phone')))
$user->phone = $req->post('phone');
if(!empty($req->post('email')))
$user->email = $req->post('email');
if(!empty($req->post('password')))
$user->password = Hash::make($req->post('password'));

$user->save();
return Response::json(['code'=>200,'message'=>'Successfully Updated
Profile']);

}
function hitAPI(Request $req)
{
$curl = curl_init();

curl_setopt_array($curl, array(
CURLOPT_URL => 'http://localhost:8000/api/create-user',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => array('name' => 'Ankit Kumar Thakur','phone' =>
'6204847381','email' => 'ankit.thakur@gmail.com','password' =>
'12345678','password_confirmation' => '12345678'),
));

$response = curl_exec($curl);

curl_close($curl);
echo $response;

}
}

You might also like