I will explain step by step tutorial how to call curl post request in laravel 8. this example will help you how to post curl request in php laravel 8. we will help you to give example of laravel 8 post http response code. We will use laravel 8 http curl post request example.
Here, i will give you two examples of how to call curl post request with laravel GuzzleHttp. first example will with http and second example with GuzzleHttp. so let's see both examples one by one here. you can easily use this example with laravel 6, laravel 7 and laravel 8 version.
Install guzzlehttp/guzzle Package:
you have to install guzzlehttp/guzzle composer package in your project:
composer require guzzlehttp/guzzle
Example 1:
<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\Http;
class ITSController extends Controller
{
/**
* Write code on Method
*
* @return response()
*/
public function index()
{
$apiURL = 'https://api.mywebtuts.com/api/users';
$postInput = [
'first_name' => 'Hardik',
'last_name' => 'Savani',
'email' => 'example@gmail.com'
];
$headers = [
'X-header' => 'value'
];
$response = Http::withHeaders($headers)->post($apiURL, $postInput);
$statusCode = $response->status();
$responseBody = json_decode($response->getBody(), true);
dd($responseBody);
}
}
Output
Array
(
[id] => 281
[first_name] => Hardik
[last_name] => Savani
[email] => example@gmail.com
[created_at] => 2021-07-29T03:51:48.693210Z
)
Example 2:
<?php
namespace App\Http\Controllers;
class ITSController extends Controller
{
/**
* Write code on Method
*
* @return response()
*/
public function index2()
{
$apiURL = 'https://api.mywebtuts.com/api/users';
$postInput = [
'first_name' => 'Hardik',
'last_name' => 'Savani',
'email' => 'example@gmail.com'
];
$client = new \GuzzleHttp\Client();
$response = $client->request('POST', $apiURL, ['form_params' => $postInput]);
$statusCode = $response->getStatusCode();
$responseBody = json_decode($response->getBody(), true);
dd($responseBody);
}
}
Output
Array
(
[id] => 281
[first_name] => Hardik
[last_name] => Savani
[email] => example@gmail.com
[created_at] => 2021-07-29T03:51:48.693210Z
)
i hope it can help you...
Do you like below Tutorials ?
- PHP Convert Date String to Datetime Object
- Laravel Validation Different Value Example
- Jquery Redirect to URL After Specific Time Example
- User Roles and Permissions in Laravel Example
- How to Get Value of Selected Option in Vue JS?
- Laravel Change Password OLD Password Validation Example
- Vue JS Get String Length Example
- How to Active and Inactive Status in Laravel?