How to Call External API in Laravel

Call external APIs in laravel 10/9/8/7 apps; In this tutorial, you will learn how to send http get, post, put and delete request to call external APIs in laravel from controller, blade and model using Http facade.

Sometimes, you need to call external APIs in laravel controller, blade, and model, So you can GET, POST, PUT, DELETE, requests with headers for that.

How to Call External APIs in Laravel Apps using Http facade

Let’s use the following methods to call external APIs from the controller, blade, and model in laravel 10/9/8/7/6 using Http facade:

  • Method 1: Laravel Call GET Request API
  • Method 2: Laravel Call POST Request API

Method 1: Laravel Call GET Request API

Let’s use the following controller method to call or send curl http get request in laravel:

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

    }

}

Method 2: Laravel Call POST Request API

Let’s use the following controller method to call or send a curl HTTP post request in laravel

<?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 tutsmake.com',

                    'body' => 'This is test from tutsmake.com as body',

                ]);



        $jsonData = $response->json();


        dd($jsonData);

    }

}

Conclusion

Call external APIs in laravel; In this tutorial, you have learned how to call external APIs in laravel from the controller.

Recommended Laravel Tutorials

AuthorDevendra Dode

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

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