How to Call External API in Laravel 9?

Hello artisan,

In this tutorial, I will show you, how to call external api in a Laravel application. I will share with you how to call external api in laravel controller. This article shows in detail how to call api in laravel 9. I will show you some simple examples of laravel call api from controller.

If we want to call an external url or api in laravel controller then we can use the HTTP facade. Laravel provides an HTTP facade and we can call API requests with GET, POST, PUT, DELETE and with headers as well using HTTP facades. Let's see all the request examples one by one. 

GET Request API Example:

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

 

POST Request API Example:

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 codecheef.org',
                    'body' => 'This is test from codecheef.org as body',
                ]);
  
        $jsonData = $response->json();
      
        dd($jsonData);
    }
}

 

PUT Request API Example:

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 codecheef.org',
                    'body' => 'This is test from codecheef.org as body',
                ]);
  
        $jsonData = $response->json();
      
        dd($jsonData);
    }
}

 

DELETE Request API Example:

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

 

Read also: Handle Exception Error in Laravel using Rescue Helper

 

Hope it can help you. Http Client in Laravel Docs: Click Here.

 

author-image
Facebook Github
A web enthusiastic, a self-motivated full-stack software engineer from Dhaka, Bangladesh with experience in developing applications using Laravel , React and Vue js