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.