Skip to content
Readerstacks logo Readerstacks
  • Home
  • Softwares
  • Angular
  • Php
  • Laravel
  • Flutter
Readerstacks logo
Readerstacks
How to Send Curl Post and Get Request with Headers in Laravel

How to Send Curl Post and Get Request with Headers in Laravel ?

Aman Jain, June 11, 2022June 11, 2022

In this article i will show you to send curl post and get request with headers in laravel. As we know curl basically a command line software which used for transferring data between servers and php provides its inbuilt library in php core.

In laravel or php 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 this article we will use a simple example to send post and get request in laravel curl. To accomplish this task we will create a project and route to send the request to third party application

So let’s start the tutorial of send curl post and get request in laravel

Method 1 : Send POST Request Using curl with headers

In this method we will send post request with Content-Type header

<?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;


});

here we used curl_setopt( $ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json')); to set the headers

laravel curl post request
laravel curl post request

Method 2 : Send GET Request Using curl with Headers

Second method is to use the Curl to send the GET request with headers

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


Route::get("/send-request",function(){
    $header = array();
     
    $header[] = 'Content-type: application/json';
    $header[] = 'Authorization: Bearer $auth_token';
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "https://api.sampleapis.com/cartoons/cartoons2D");
    curl_setopt( $ch, CURLOPT_HTTPHEADER, $header);
    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;


});

Here we added Authorization header in request header

Output :

Php curl
Php curl

Related

Php Laravel Laravel 9 laravelpost

Post navigation

Previous post
Next post

Related Posts

Php How to Send Mail in Laravel Through Sendgrid

How to Send Mail in Laravel 8 / 9 Through Sendgrid ?

May 10, 2022May 10, 2022

Sendgrid Provides multiple features to send the email like bulk email, marketing email , APIs, Delivery reports, Templates and many more. In that way send gird is much interesting and capable of performing many tasks related to marketing and bulk mailing. So in this article we will learn to use…

Read More
Php Create Custom Class or Custom Library in Laravel 11

Create Custom Class or Custom Library in Laravel 11

July 6, 2024July 6, 2024

Custom classes and libraries in Laravel can streamline your application’s codebase by encapsulating common functionality and business logic. This tutorial guides you through creating and utilizing custom classes using Laravel’s powerful features. These classes can be used to encapsulate common functionality, business logic, or to abstract away complex operations. In…

Read More
Php How to Change Date Format in Blade View File in Laravel

How to Change Date Format in Blade View File in Laravel ?

September 8, 2022March 16, 2024

Laravel default shows the date in the way its stored in database bu sometimes we want to change date format in blade view file in laravel according to our requirements. It can be multiple possibilities and requirement from client or product manager to change the date format so in this…

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

  • June 2025
  • 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

  • The Resilience of Nature: How Forests Recover After Fires
  • Understanding Laravel Cookie Consent for GDPR Compliance
  • Understanding High Vulnerabilities: A Critical Overview of the Week of May 12, 2025
  • Installing a LAMP Stack on Ubuntu: A Comprehensive Guide
  • Understanding High Vulnerabilities: A Deep Dive into Recent Security Concerns
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version