Spread the love

In this article we will learn to call an post rest api in Laravel. Whenever we want to access the third party data we need to access the data using the APIs, we send a request to another server means outside our application and they respond with preformatted structure. Post request is used to send bulk data and upload the files from client to server and here we are going to send to post request from one server to another server.

In laravel there is multiple ways to call the Rest APIs Url using the file_get_contents, curl or Guzzel package. file_get_contents is php core function which we can use to call the external api both Post and Get. curl basically a command line software which used for transferring data between servers and php provides its inbuilt library in php core then final Guzzel is a package which is a http client makes easy to send HTTP requests.

In this article i will use a simple example to send the Rest post request using both these 3 methods and will use a controller and routes.

So let’s start the tutorial of call an post rest api in Laravel

Send POST HTTP Request in Laravel

In this example we will send a GET request to another server using three methods as follow

Method 1 : Send Request Using file_get_contents

This one is easiest method to send the request to external server

<?php 
use Illuminate\Support\Facades\Route;


Route::get("/send-request",function(){

   //if want to send request like form
   // $postdata =  json_encode(array( 
   //     "name"=> "morpheus",
  //      "job"=>"leader"
  //  ));
    
    $opts = array('http' =>
        array(
            'method'  => 'POST',
            'header'  => 'Content-Type: application/json',
            // 'header'  => 'Content-Type: application/x-www-form-urlencoded', if want to send request like form
            'content' => $postdata
        )
    );
    
    $context  = stream_context_create($opts);
    
    $result = file_get_contents('https://reqres.in/api/users', false, $context);
    dump($http_response_header);
    dump(json_decode($result));
    return 1;
});

Output :

php post request
php post request

Method 2 : Send Request Using curl

Second method is to use the Curl to send the request

<?php 
use Illuminate\Support\Facades\Route;


Route::get("/send-request",function(){

     $postdata = json_encode(
        array(
            "name"=> "morpheus",
            "job"=>"leader"
        )
    );
     $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "https://reqres.in/api/users");
    curl_setopt( $ch, CURLOPT_POSTFIELDS, $postdata );
    curl_setopt( $ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $output = curl_exec($ch);
    $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);

    dump($httpcode);
    dump(json_decode($output));
    return $output;


});

Output :

laravel curl post request
laravel curl post request

Method 3 : Send Request Using Guzzel

Third method is to use the Guzzel package to send the request

<?php 
use Illuminate\Support\Facades\Route;


Route::get("/send-request",function(){

 $postdata = array(
            "name"=> "morpheus",
            "job"=>"leader"
        );

$client = new \GuzzleHttp\Client();
$res = $client->post('https://reqres.in/api/users',
[
    \GuzzleHttp\RequestOptions::JSON => $postdata // or 'json' => [...]
]);
dump( $res->getStatusCode()); // 200
echo (  $res->getBody());
return 1;


});

Output :

laravel curl post request
laravel guzzel post request

Leave a Reply