Laravel 8 Http cURL Get Request Tutorial

Reading Time: 4 minutes
9,182 Views

Inside this article we will see the concept of Laravel 8 Http cURL Get Request Tutorial. We will see by both using Http GET and GuzzleHttp of laravel to process JSON data.

The cURL stands for ‘Client for URLs‘, originally with URL spelled in uppercase to make it obvious that it deals with URLs. Since curl uses libcurl, it supports a range of common internal protocols, currently including HTTP, HTTPS, FTP, FTPS, GOPHER, TELNET, DICT, and FILE.

This article is very easy to learn and implement in your development. We will look at example of laravel curl get request.

Learn More –

Let’s get started.


Laravel Installation

We will create laravel project using composer. So, please make sure your system should have composer installed. If not, may be this article will help you to Install composer in system.

Here is the command to create a laravel project-

composer create-project --prefer-dist laravel/laravel blog

To start the development server of Laravel –

php artisan serve

URL: http://127.0.0.1:8000

Assuming laravel already installed inside your system.


Create Controller & Add HTTP cURL

Open project into terminal and run this artisan command.

$ php artisan make:controller SiteController

It will create SiteController.php inside /app/Http/Controllers folder.

Open SiteController.php and write this code into it.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;

class SiteController extends Controller
{
    /**
     * Using Http
     *
     * @return response()
     */
    public function index()
    {
        $apiURL = 'https://jsonplaceholder.typicode.com/posts';
  
        $response = Http::get($apiURL);
  
        $statusCode = $response->status();
        $responseBody = json_decode($response->getBody(), true);
  
        dd($responseBody);
    }

    /**
     * Using GuzzleHttp
     *
     * @return response()
     */
    public function posts()
    {
        $apiURL = 'https://jsonplaceholder.typicode.com/posts';
              
        $client = new \GuzzleHttp\Client();
        $response = $client->request('GET', $apiURL);
   
        $statusCode = $response->getStatusCode();
        $responseBody = json_decode($response->getBody(), true);
  
        dd($responseBody);
    }
}

This controller includes two methods to process HTTP cURL request – Using HTTP and using GuzzleHttp. You can opt any option to use it in your development.


Add Route

Open web.php file from /routes folder. Add these routes into it.

//...

Route::get("posts", [SiteController::class, "index"]);
Route::get("all-posts", [SiteController::class, "posts"]);

//...

Application Testing

Run this command into project terminal to start development server,

php artisan serve

Example URL #1 – http://127.0.0.1:8000/posts

Example URL #2 – http://127.0.0.1:8000/all-posts

We hope this article helped you to learn Laravel 8 Http cURL Get Request Tutorial in a very detailed way.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.

Sanjay KumarHello friends, I am Sanjay Kumar a Web Developer by profession. Additionally I'm also a Blogger, Youtuber by Passion. I founded Online Web Tutor and Skillshike platforms. By using these platforms I am sharing the valuable knowledge of Programming, Tips and Tricks, Programming Standards and more what I have with you all. Read more