How to Make External API calls in Laravel? – GET, POST, PUT, DELETE

In the modern web development era, integrating external APIs in a Laravel application has become a vital aspect. In this tutorial, you will learn how to call an external API in a Laravel controller. We will be covering various examples of sending HTTP API requests, such as GET, POST, PUT, and DELETE, using Laravel’s Http…

By.

•

min read

In the modern web development era, integrating external APIs in a Laravel application has become a vital aspect. In this tutorial, you will learn how to call an external API in a Laravel controller. We will be covering various examples of sending HTTP API requests, such as GET, POST, PUT, and DELETE, using Laravel’s Http facade. This tutorial will be helpful for Laravel 6, 7, 8, 9, and 10 versions.

To call an external API in a Laravel controller, we will utilize the Http facade provided by Laravel. This facade allows us to send API requests with GET, POST, PUT, DELETE, and headers.

Now, let’s dive into the details with step-by-step examples.

[lwptoc]

 

Requirements and Installation

Before we start, ensure you have Laravel installed. If not, you can create a new Laravel app using the following command:

composer create-project laravel/laravel example-app

 

Laravel Call GET Request API Example

In this example, we will demonstrate how to send a GET request API in Laravel. Update your route file and controller file with the following code:

// routes/web.php

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

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

// app/Http/Controllers/PostsController.php

namespace App\Http\Controllers;

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

class PostsController extends Controller
{
    public function index()
    {
        $response = Http::get('https://jsonplaceholder.typicode.com/posts');
        $jsonData = $response->json();
        dd($jsonData);
    }
}

 

Laravel Call POST Request API Example

Next, let’s see how to send a POST request API in Laravel. Update your route file and controller file with the following code:

// routes/web.php

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

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

// app/Http/Controllers/PostsController.php

namespace App\Http\Controllers;

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

class PostsController extends Controller
{
    public function store()
    {
        $response = Http::post('https://jsonplaceholder.typicode.com/posts', [
            'title' => 'This is a test from LaravelTutorial.com',
            'body' => 'This is a test from LaravelTutorial.com as body',
        ]);

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

 

Laravel Call PUT Request API Example

Now, we will see how to send a PUT request API in Laravel. Update your route file and controller file with the following code:

// routes/web.php

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

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

// app/Http/Controllers/PostsController.php

namespace App\Http\Controllers;

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

class PostsController extends Controller
{
    public function update()
    {
        $response = Http::put('https://jsonplaceholder.typicode.com/posts/1', [
            'title' =>'this is a test from LaravelTutorial.com',
'body' => 'This is a test from LaravelTutorial.com as body',
]);    
             $jsonData = $response->json();
            dd($jsonData);
     }
}

 

Laravel Call DELETE Request API Example

In this example, we will demonstrate how to send a DELETE request API in Laravel. Update your route file and controller file with the following code:

// routes/web.php

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

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

// app/Http/Controllers/PostsController.php

namespace App\Http\Controllers;

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

class PostsController extends Controller
{
    public function delete()
    {
        $response = Http::delete('https://jsonplaceholder.typicode.com/posts/1');
        $jsonData = $response->json();
        dd($jsonData);
    }
}

 

Laravel API with Response

Finally, let‘s create a comprehensive example of an HTTP request with Laravel. Update your route and controller files with the following code:

// routes/web.php

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

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

// app/Http/Controllers/PostsController.php

namespace App\Http\Controllers;

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

class PostsController 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());
    }
}

 

Conclusion

In this tutorial, we have covered various examples of calling external APIs in a Laravel controller using the Http facade. These examples included GET, POST, PUT, and DELETE request APIs. We hope this tutorial helps you with your Laravel projects!

FAQs

 

How do I call an external API in Laravel?

You can use Laravel’s Http facade to call external APIs in a Laravel controller.

 

What Laravel versions are covered in this tutorial?

This tutorial is applicable to Laravel 6, 7, 8, 9, and 10 versions.

 

How do I send API requests with headers in Laravel?

You can use the withHeaders() method provided by the Http facade to send API requests with headers.

 

Can I use this tutorial for other HTTP methods?

Yes, this tutorial provides examples for GET, POST, PUT, and DELETE methods. You can modify the examples according to your needs.

 

How can I handle API responses in Laravel?

You can handle API responses using the json(), status(), ok(), successful(), serverError(), and clientError() methods provided by the Http facade.

Leave a Reply

Your email address will not be published. Required fields are marked *