How to Call External API in Laravel?

Hi Artisan,

In this tutorial, you will discover how to call api in laravel controller. I would like to share with you how to call external api in laravel controller. This article goes in detailed on how to call api in laravel. It’s a simple example of laravel call api from controller. So, let us dive into the details.

You can use this example with laravel 6, laravel 7, laravel 8 and laravel versions.

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.

Here, I will give you very simple examples of how to run a call HTTP API request from laravel?

1) Laravel Call GET Request API Example.medrectangle-3-multi-157{border:none!important;display:block!important;float:none!important;line-height:0;margin-bottom:15px!important;margin-left:0!important;margin-right:0!important;margin-top:15px!important;max-width:100%!important;min-height:250px;min-width:300px;padding:0;text-align:center!important}

2) Laravel Call POST Request API Example

3) Laravel Call PUT Request API Example

4) Laravel Call DELETE Request API Example

5) Laravel API with Response

Let’s see one by one example:

Install Laravel:

This step is not required; however, if you have not created the laravel app, then you may go ahead and execute the below command:.medrectangle-4-multi-155{border:none!important;display:block!important;float:none!important;line-height:0;margin-bottom:15px!important;margin-left:0!important;margin-right:0!important;margin-top:15px!important;max-width:100%!important;min-height:250px;min-width:300px;padding:0;text-align:center!important}

composer create-project laravel/laravel example-app

1) Laravel Call GET Request API Example:

Here, we will see how to send curl http get request in laravel, let’s update route file code and controller file code. you can see output as well:

routes/web.php

<?php

use IlluminateSupportFacadesRoute;

use AppHttpControllersPostController;

/*

|--------------------------------------------------------------------------

| Web Routes

|--------------------------------------------------------------------------

|

| Here is where you can register web routes for your application. These

| routes are loaded by the RouteServiceProvider within a group which

| contains the "web" middleware group. Now create something great!

|

*/

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

app/Http/Controllers/PostController.php

<?php

namespace AppHttpControllers;

use IlluminateHttpRequest;

use IlluminateSupportFacadesHttp;

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

}

}

Output:

How to Call External API in Laravel?

2) Laravel Call POST Request API Example:

Here, we will see how to send curl http post request API in laravel, let’s update route file code and controller file code. you can see output as well:

routes/web.php

<?php

use IlluminateSupportFacadesRoute;

use AppHttpControllersPostController;

/*

|--------------------------------------------------------------------------

| Web Routes

|--------------------------------------------------------------------------

|

| Here is where you can register web routes for your application. These

| routes are loaded by the RouteServiceProvider within a group which

| contains the "web" middleware group. Now create something great!

|

*/

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

app/Http/Controllers/PostController.php

<?php

namespace AppHttpControllers;

use IlluminateHttpRequest;

use IlluminateSupportFacadesHttp;

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

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

]);

$jsonData = $response->json();

dd($jsonData);

}

}

Output:

How to Call External API in Laravel?

3) Laravel Call PUT Request API Example:

Here, we will see how to send curl http put request API in laravel, let’s update route file code and controller file code. you can see output as well:

routes/web.php

<?php

use IlluminateSupportFacadesRoute;

use AppHttpControllersPostController;

/*

|--------------------------------------------------------------------------

| Web Routes

|--------------------------------------------------------------------------

|

| Here is where you can register web routes for your application. These

| routes are loaded by the RouteServiceProvider within a group which

| contains the "web" middleware group. Now create something great!

|

*/

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

app/Http/Controllers/PostController.php

<?php

namespace AppHttpControllers;

use IlluminateHttpRequest;

use IlluminateSupportFacadesHttp;

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

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

]);

$jsonData = $response->json();

dd($jsonData);

}

}

Output:

How to Call External API in Laravel?

4) Laravel Call DELETE Request API Example:

Here, we will see how to send curl http delete request API in laravel, let’s update route file code and controller file code. you can see output as well:

routes/web.php

<?php

use IlluminateSupportFacadesRoute;

use AppHttpControllersPostController;

/*

|--------------------------------------------------------------------------

| Web Routes

|--------------------------------------------------------------------------

|

| Here is where you can register web routes for your application. These

| routes are loaded by the RouteServiceProvider within a group which

| contains the "web" middleware group. Now create something great!

|

*/

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

app/Http/Controllers/PostController.php

<?php

namespace AppHttpControllers;

use IlluminateHttpRequest;

use IlluminateSupportFacadesHttp;

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

}

}

5) Laravel API with Response:

We will create very simple http request full example. we need to create simple route to call controller method. so let’s create it:

routes/web.php.leader-1-multi-159{border:none!important;display:block!important;float:none!important;line-height:0;margin-bottom:15px!important;margin-left:0!important;margin-right:0!important;margin-top:15px!important;max-width:100%!important;min-height:250px;min-width:300px;padding:0;text-align:center!important}

<?php

use IlluminateSupportFacadesRoute;

use AppHttpControllersPostController;

/*

|--------------------------------------------------------------------------

| Web Routes

|--------------------------------------------------------------------------

|

| Here is where you can register web routes for your application. These

| routes are loaded by the RouteServiceProvider within a group which

| contains the "web" middleware group. Now create something great!

|

*/

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

app/Http/Controllers/PostController.php

<?php

namespace AppHttpControllers;

use IlluminateHttpRequest;

use IlluminateSupportFacadesHttp;

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

}

}

Output:

status:200

ok:1

successful:1

serverError:

clientError:

headers:Array

(

[Date] => Array

(

[0] => Thu, 12 Mar 2020 06:08:58 GMT

)

[Content-Type] => Array

(

[0] => application/json; charset=utf-8

)

[Transfer-Encoding] => Array

(

[0] => chunked

)

.....

)

You can also get more information about Http Client in Laravel Docs: Click Here.

I hope it can help you…

Leave a Comment