How to Fetch Data from an API in Laravel

How to Fetch Data from an API in Laravel | 100% Work

How to Fetch Data from an API in Laravel. In this guide, we are going to learn laravel redirect external url. In this article, we will implement a laravel redirect()->away. This post will give you a simple example of laravel redirect to external url from controller. I explained simply about laravel redirect to external link. Alright, let us dive into the details.

To fetch data from an API in Laravel, you can use Laravel’s built-in Http client, available via the Illuminate\Support\Facades\Http facade. First, make a GET request using Http::get('API_URL'). This will return a response object, which can be checked for success using successful(). The response body can be retrieved as JSON using the json() method. You can also send headers, set timeouts, and retry failed requests. Laravel provides convenient methods for handling errors and sending different types of requests, such as POST, PUT, or DELETE.

You can use this example with laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11 versions. You Can Learn Laravel 11 Reverb Real-Time Notifications

Here, I will give you very simple examples of How to Fetch Data from an API in Laravel

1) Laravel Call GET Request API Example

2) Laravel Call POST Request API Example

3) Laravel Call PUT Request API Example

4) Laravel Call DELETE Request API Example

5) Laravel API with Response

Let’s see one by one example:

Install Laravel:

This step is not required; however, if you have not created the laravel app, then you may go ahead and execute the below command:

composer create-project laravel/laravel example-app

1) Laravel Call GET Request API Example:

Here, we will see how to send curl http get request in laravel, let’s update route file code and controller file code. you can see output as well:

routes/web.php

<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\PostController;



/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/



Route::get('posts', [PostController::class, 'index']);

app/Http/Controllers/PostController.php

<?php

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;

class PostController extends Controller

{

/**
* Write code on Method
*
* @return response()
*/

public function index()

{
$response = Http::get('https://jsonplaceholder.typicode.com/posts');
$jsonData = $response->json();
dd($jsonData);

}

}

Output:

How to Fetch Data from an API in Laravel

2) Laravel Call POST Request API Example:

Here, we will see how to send curl http post request API in laravel, let’s update route file code and controller file code. you can see output as well:

routes/web.php

<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\PostController;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('posts/store', [PostController::class, 'store']);

app/Http/Controllers/PostController.php

<?php

namespace App\Http\Controllers;


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

class PostController extends Controller

{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function store()

    {
        $response = Http::post('https://jsonplaceholder.typicode.com/posts', [
                    'title' => 'This is test from DevScriptSchool.Com',
                    'body' => 'This is test from DevScriptSchool.Com as body',
                ]);
        $jsonData = $response->json();
        dd($jsonData);

    }

}

Output:

3) Laravel Call PUT Request API Example:

Here, we will see how to send curl http put request API in laravel, let’s update route file code and controller file code. you can see output as well:

routes/web.php

<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\PostController;

  
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|

*/

  

Route::get('posts/update', [PostController::class, 'update']);

app/Http/Controllers/PostController.php

<?php

namespace App\Http\Controllers;

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


class PostController extends Controller

{
    /**
     * Write code on Method
     *
     * @return response()
     */

    public function update()

    {
        $response = Http::put('https://jsonplaceholder.typicode.com/posts/1', [
                    'title' => 'This is test from DevScriptSchool.Com',
                    'body' => 'This is test from DevScriptSchool.Com as body',
                ]);

        $jsonData = $response->json();
        dd($jsonData);

    }

}

Output:

4) Laravel Call DELETE Request API Example:

Here, we will see how to send curl http delete request API in laravel, let’s update route file code and controller file code. you can see output as well:

routes/web.php

<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\PostController;


/*

|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!

|

*/

  

Route::get('posts/delete', [PostController::class, 'delete']);

app/Http/Controllers/PostController.php

<?php

namespace App\Http\Controllers;


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

class PostController extends Controller

{

    /**
     * Write code on Method
     *
     * @return response()
     */

    public function delete()

    {

        $response = Http::delete('https://jsonplaceholder.typicode.com/posts/1');
        $jsonData = $response->json();

        dd($jsonData);

    }

}

5) Laravel API with Response:

We will create very simple http request full example. we need to create simple route to call controller method. so let’s create it:

routes/web.php

<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\PostController;

  

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|

*/

  

Route::get('posts', [PostController::class, 'index']);

app/Http/Controllers/PostController.php

<?php


namespace App\Http\Controllers;

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


class PostController extends Controller

{

    public function index()
    {
        $response = Http::withHeaders([
            'Authorization' => 'token' 
        ])->get('http://jsonplaceholder.typicode.com/posts');
        $jsonData = $response->json();

     
        echo "<pre> status:";
        print_r($response->status());
        echo "<br/> ok:";
        print_r($response->ok());
        echo "<br/> successful:";
        print_r($response->successful());
        echo "<br/> serverError:";
        print_r($response->serverError());
        echo "<br/> clientError:";
        print_r($response->clientError());
        echo "<br/> headers:";
        print_r($response->headers());
    }

}

You can also get more information about Http Client in Laravel Docs: Click Here.

I hope it can help you…

This Post Has One Comment

Leave a Reply