Skip to content
Readerstacks logo Readerstacks
  • Home
  • Softwares
  • Angular
  • Php
  • Laravel
  • Flutter
Readerstacks logo
Readerstacks
How to Call a POST Rest Api in Laravel

How to Call a POST Rest Api in Laravel ?

Aman Jain, June 7, 2022June 7, 2022

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

Related

Php Laravel Laravel 9 callhttpphp

Post navigation

Previous post
Next post

Related Posts

Php Ajax laravel Image Upload

Ajax laravel Image Upload with form with example

June 6, 2022November 6, 2023

In this tutorial i will show you to use Ajax laravel image Upload with form in laravel . This can be implement easily using the laravel file and storage providers. in our recent articles of How to Upload image with preview in Laravel 8 with example ? i explained to…

Read More
Php How to Permanent Delete Soft Deleted Records in Laravel 9

How to Permanent Delete Soft Deleted Records in Laravel 9 ?

June 18, 2022June 18, 2022

In this article we will learn to Permanent Delete soft deleted records in Laravel. In our recent article How to fetch Soft Deleted Records in Laravel 9 ? and How to Restore Soft Deleted Records in Laravel 9 we learnt to fetch the records from database and Restore the records…

Read More
Php Create multi language website in Laravel

How to create multi language website in Laravel 8 / 9 with example?

November 25, 2021May 10, 2022

Laravel provides its own localization library to handle the multi language feature in application. Laravel stores each languages translations in resource/lang . If you are going to build a full featured multi language website then you are at right place to end your search. In this article i will show…

Read More

Aman Jain
Aman Jain

With years of hands-on experience in the realm of web and mobile development, they have honed their skills in various technologies, including Laravel, PHP CodeIgniter, mobile app development, web app development, Flutter, React, JavaScript, Angular, Devops and so much more. Their proficiency extends to building robust REST APIs, AWS Code scaling, and optimization, ensuring that your applications run seamlessly on the cloud.

Categories

  • Angular
  • CSS
  • Dart
  • Devops
  • Flutter
  • HTML
  • Javascript
  • jQuery
  • Laravel
  • Laravel 10
  • Laravel 11
  • Laravel 9
  • Mysql
  • Php
  • Softwares
  • Ubuntu
  • Uncategorized

Archives

  • May 2025
  • April 2025
  • October 2024
  • July 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • July 2023
  • March 2023
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021

Recent Posts

  • Understanding High Vulnerabilities: A Deep Dive into Recent Security Concerns
  • Understanding High Vulnerabilities in Software: A Week of Insights
  • Blocking Spam Requests with LaraGuard IP: A Comprehensive Guide
  • Enhancing API Development with Laravel API Kit
  • Exploring the Future of Web Development: Insights from Milana Cap
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version