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 ?
- CRUD Laravel 7.x and 6.x Example
- Image Upload Laravel 7.x and 6.x Tutorial
- Laravel 7.x and 6.x Delete File from public folder example
- How to delete directory in Laravel 7.x and 6.x?
- How to create directory if not exists in Laravel 7.x and 6.x?
- Laravel 7.x and 6.x get all files in directory example
- Laravel 7.x and 6.x check file exists in folder example
- Ajax Autocomplete Textbox in Laravel 7.x and 6.x Example