Laravel API Tutorial | Register User in multiple websites at same time
In Part-26 of Laravel 8 API Tutorial, we will start working on the API Registration process to register Users on multiple websites at the same time.
Sometimes we require to register the user at multiple websites at the same time. At that time, we require to build a Registration API for both the websites that will register users at both websites.
Like:-
If a user registers at the 1st website then the user will also get registered to the 2nd website automatically with API.
We have 2 Laravel E-commerce websites, one is stackdevelopers.in and another one is stack-developers.xyz
When a user registers at stackdevelopers.in then the user will also get register at stack-developers.xyz and when a user registers at stack-developers.xyz then the user will also register at stackdevelopers.in
In this video, we will do this task offline. We will run both basic and advance e-commerce websites offline and register at both websites when user register at any of the website.
1st website (Update at Advance E-commerce Website)
1) Update registerUser function
At register function, after saving user, create curl to pass user data
to 2nd website.
// Curl Request to Create User in Basic E-commerce Website
$postdata = json_encode($data);
$url = ‘http://127.0.0.1:8010/api/register-user’;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(‘Content-Type: application/json’));
$result = curl_exec($ch);
curl_close($ch);
/*print_r ($result); die;*/
2nd website (Update at Basic E-commerce Website)
2) Create Route:-
Create post route to register user at basic e-commerce website in api.php file like below :-
// Register User from Laravel Advance E-commerce
Route::match([‘get’,’post’],’/register-user’, ‘APIController@registerUser’);
3) Create registerUser function :-
Now, create registerUser function at APIController to register the user at basic e-commerce website from advance e-commerce website.