How To Work with External API in Laravel 9 Tutorial

Share this Article
Reading Time: 6 minutes
535 Views

Inside this article we will see the concept i.e How To Work with External API in Laravel 9 Tutorial. Article contains the classified information about How to call external api in Laravel.

If you are looking for a solution i.e How to consume an external API with Laravel using HTTP Facade then this article will help you a lot for this. Tutorial is super easy to understand and implement it in your code as well.

If you want to call external url or api in laravel controller then we can use Http facade of laravel. using Http facade we can call API request with GET, POST, PUT, DELETE and with headers as well.

Learn More –

Let’s get started.

Laravel Installation

Open terminal and run this command to create a laravel project.

$ composer create-project laravel/laravel myblog

It will create a project folder with name myblog inside your local system.

To start the development server of laravel –

$ php artisan serve

URL: http://127.0.0.1:8000

Assuming laravel already installed inside your system.

Laravel Call GET Request API Example

Here, we will see how to send curl http get request to get all resources in laravel.

Create Controller

Open project into terminal and run this command.

$ php artisan make:controller DataController

It will create a DataController.php file inside /app/Http/Controllers folder.

Open DataController.php file and write this code into it.

<?php

namespace App\Http\Controllers;

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

class DataController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function index()
    {
        $response = Http::get('https://api.onlinewebtutorblog.com/employees');

        $jsonData = $response->json();

        dd($jsonData);
    }
}

Add Route

Open web.php from /routes folder and add this route into it.

//...

use App\Http\Controllers\DataController;

Route::get('data', [DataController::class, 'index']);

Output

URL: http://127.0.0.1:8000/data

Laravel Call POST Request API Example

Here, we will see how to send curl http post with request body to create a new resource in laravel.

Open DataController.php file and write this code into it.

<?php

namespace App\Http\Controllers;

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

class DataController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function store()
    {
        $response = Http::post('https://api.onlinewebtutorblog.com/employees', [
            'username' => 'sanjay_owt',
            'name' => 'Sanjay Kumar',
            'email' => 'onlinewebtutorhub@gmail.com',
            'gender' => 'Male',
            'designation' => 'Web Developer',
            'phone_number' => '1234567890',
            'complete_address' => 'Sample address, sample location, 158975'
        ]);

        $jsonData = $response->json();

        dd($jsonData);
    }
}

Add Route

Open web.php from /routes folder and add this route into it.

//...

use App\Http\Controllers\DataController;

Route::get('store', [DataController::class, 'store']);

Output

URL: http://127.0.0.1:8000/store

Laravel Call PUT Request API Example

Here, we will see how to send curl http put with request body to update resource in laravel.

Open DataController.php file and write this code into it.

<?php

namespace App\Http\Controllers;

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

class DataController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function update()
    {
        $response = Http::put('https://api.onlinewebtutorblog.com/employees/1', [
            'name' => 'Sanjay Kumar',
            'email' => 'onlinewebtutorhub@gmail.com',
            'gender' => 'Male',
            'designation' => 'Web Developer'
        ]);

        $jsonData = $response->json();

        dd($jsonData);
    }
}

Add Route

Open web.php from /routes folder and add this route into it.

//...

use App\Http\Controllers\DataController;

Route::get('update', [DataController::class, 'update']);

Output

URL: http://127.0.0.1:8000/update

Laravel Call DELETE Request API Example

Here, we will see how to send curl http delete to delete a resource in laravel.

Open DataController.php file and write this code into it.

<?php

namespace App\Http\Controllers;

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

class DataController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function destroy()
    {
        $response = Http::delete('https://api.onlinewebtutorblog.com/employees/1');

        $jsonData = $response->json();

        dd($jsonData);
    }
}

Add Route

Open web.php from /routes folder and add this route into it.

//...

use App\Http\Controllers\DataController;

Route::get('delete', [DataController::class, 'destroy']);

Output

URL: http://127.0.0.1:8000/delete

We hope this article helped you to learn How To Work with External API in Laravel 9 Tutorial in a very detailed way.

Buy Me a Coffee

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

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.

Was this post helpful?

PHP Web Development Courses on Offer in just $4
PHP Web Development Courses on Offer in just $4