Site Admin
Site Admin Founder of MeaningArticles
1203 Views

Laravel Http Curl Post Request with Headers

Hello Dev.

Are you searching out instance of laravel http curl post request instance. this article will give you simple example of laravel curl post request with headers example. you'll learn laravel http request post parameters. this article is going in designated on how to call curl post request in laravel. follow bellow step for a way to post curl request in php laravel.

Here, i will give you examples of ways to call curl post request with laravel GuzzleHttp. first instance will with http and second example with GuzzleHttp. so let's examine each examples one at a time right here. you may effortlessly use this case 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' => 'Hrdk',
            'last_name' => 'Sovona',
            'email' => '[email protected]'
        ];
  
        $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] => Hrdk
    [last_name] => Sovona
    [email] => [email protected]
    [created_at] => 2021-08-15T03: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' => 'Hrdk',
            'last_name' => 'Sovona',
            'email' => '[email protected]'
        ];
                
        $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] => Hrdk
    [last_name] => Sovona
    [email] => [email protected]
    [created_at] => 2021-07-29T03:51:48.693210Z
)

i'm hoping it assist you to, thanks for visit my article if you like my article then proportion together with your friend and social platform.