Laravel provides a powerful HTTP Client for making API requests, simplifying the process of sending and receiving data from external services.
In this blog post, we will explore different HTTP methods, sending requests with query parameters and JSON data, and handling API responses.
1. Understanding HTTP Methods
When interacting with APIs, different HTTP methods are used based on the type of request:
- GET – Retrieve data from an API
- POST – Send data to an API to create a new record
- PUT – Update an existing record
- DELETE – Remove data from an API
- PATCH – Partially update a record
- OPTIONS – Check available methods on the API
- HEAD – Fetch headers without retrieving the full response body
2. Sending API Requests in Laravel
To make API requests, Laravel provides the Http
facade. Before using it, ensure you have imported it at the top of your controller: use Illuminate\Support\Facades\Http;
3. Fetching Data Using GET Request
Let’s create a controller that retrieves user data from an API and displays it in a Blade view.
Step 1: Create a Controller
Run the following Artisan command to create a controller: php artisan make:controller UserController
Then, open app/Http/Controllers/UserController.php
and modify it as follows:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;
class UserController extends Controller
{
public function fetchUsers()
{
// Fetch data from API
$response = Http::get('https://dummyjson.com/users');
// Convert response to an array
$data = $response->json();
// Pass data to view
return view('users', ['users' => $data['users']]);
}
}
Step 2: Define a Route
In routes/web.php
, add a route to call the fetchUsers
method:
use App\Http\Controllers\UserController;
Route::get('/users', [UserController::class, 'fetchUsers']);
Step 3: Create a Blade View
Now, create a Step 2: Define a Route
In routes/web.php
, add a route to call the fetchUsers
method:
use App\Http\Controllers\UserController;
Route::get('/users', [UserController::class, 'fetchUsers']);
Blade file resources/views/users.blade.php
to display the users in a table:
<!DOCTYPE html>
<html>
<head>
<title>User List</title>
</head>
<body>
<h2>User List</h2>
<table border="1">
<tr>
<th>ID</th>
<th>Name</th>
<th>Email</th>
</tr>
@foreach($users as $user)
<tr>
<td>{{ $user['id'] }}</td>
<td>{{ $user['firstName'] }} {{ $user['lastName'] }}</td>
<td>{{ $user['email'] }}</td>
</tr>
@endforeach
</table>
</body>
</html>
4. Sending Data Using POST Request
To save data through an API, we use the POST
method. Here’s an example:
$response = Http::post('https://dummyjson.com/users/add', [
'firstName' => 'John',
'lastName' => 'Doe',
'email' => 'john.doe@example.com'
]);
$data = $response->json();
5. Deleting Data Using DELETE Request
To delete a user, use the DELETE
method:
$response = Http::delete('https://dummyjson.com/users/1');
$data = $response->json();
Conclusion
Laravel’s HTTP Client makes API communication seamless. In this guide, we covered:
- Various HTTP methods
- Fetching data using
GET
- Displaying API data in a Blade template
- Sending data using
POST
- Deleting data using
DELETE
With these techniques, you can integrate external APIs into your Laravel applications efficiently!