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

file_get_contents Post and Get Request with Headers in Laravel ?

Aman Jain, June 13, 2022June 13, 2022

In this article i will show you to send file_get_contents post and get request with headers in laravel. As we know file_get_contents is php inbuilt core function used to access the local file and remote URLs. File_get_contents is used to read the file in different modes and to send the request on remote sites with header context.

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 file_get_contents. 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 file_get_contents Post and Get Request with Headers in laravel

Method 1 : Send POST Request Using file_get_contents 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(){

   //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;
});

here we used 'header' => 'Content-Type: application/json', to set the headers or set Bearer Authorization

$opts = array('http' =>
        array(
            'method'  => 'POST',
            'header'  => 'Authorization: Bearer $auth_token',
            'content' => $postdata
        )
    );
laravel curl post request
laravel curl post request

Method 2 : Send GET Request Using file_get_contents 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(){

    $response = file_get_contents("https://api.sampleapis.com/cartoons/cartoons2D");
    dump($http_response_header);
    dump(json_decode($response));
    return $response;
});

Output :

Php curl
Php curl

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

Related

Php Laravel Laravel 9 headerlaravelpost

Post navigation

Previous post
Next post

Related Posts

Php How to create multiple size thumbs from image in laravel

How to create multiple size thumbs from image in laravel 9 ?

May 14, 2022February 22, 2024

Resizing images can be a best performance booster for any application since this will load the proper size images. In this article i will show you to create multiple size thumbs from image in laravel using intervention package. Big size of image can reduce the performance of the application therefor…

Read More
Php How to compress and reduce image size while uploading in laravel

How to compress and reduce image size while uploading in laravel 8 ?

May 14, 2022May 14, 2022

While uploading the images in our application user can upload big size image but storing and rendering big size of image can reduce the performance of the application therefor its an important aspect to reduce the size of image without resizing it so we can keep the original size and…

Read More
Php Simplest Way to Use Roles and Permission in Laravel

Simplest Way to Use Roles and Permission in Laravel

August 23, 2022March 16, 2024

Roles and permission in laravel are used to provide roles based authentication and in this post we will learn simplest wat to use roles and permission in laravel. In our application we want features and modules on basis of roles or permissions so that we can restrict users to access…

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

  • Installing a LAMP Stack on Ubuntu: A Comprehensive Guide
  • 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
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version