Are you looking to connect your Laravel application with an external API? You have come to the right place! Laravel provides an easy and efficient way to make HTTP requests to external APIs with its built-in HTTP client.
This blog post will explore using Laravel's HTTP client to communicate with external APIs, handle responses and errors, and work with data in popular formats like JSON. Whether building a RESTful API or integrating with a third-party service, knowing how to make HTTP requests in Laravel is essential for any web developer.
So, get ready to learn how to make Laravel HTTP request to external API like a pro!
GET Request
use Illuminate\Support\Facades\Http;
$response = Http::get('https://jsonplaceholder.typicode.com/users');
if ($response->ok()) {
$users = $response->json();
dd($users);
} else {
dd('Could not get users');
}
Using Laravel's built-in HTTP client, we make an HTTP GET request to the URL 'https://jsonplaceholder.typicode.com/users'.
The response from the server is then stored in the $response
variable. The Http::get()
method returns an instance of Illuminate\Http\Client\Response
which can be used to inspect the response.
The if
statement checks if the response was successful by calling the ok()
method on the $response
object. If the response was successful (i.e., the status code is between 200 and 299), the code retrieves the response body as JSON by calling the json()
method on the $response
object, which returns an array of users. Finally, the dd()
function is used to dump and die the $users
array to the browser for inspection.
If the ok()
method returns false
, the else
block executes and the message "Could not get users" is displayed using the dd()
function.
POST Request
use Illuminate\Support\Facades\Http;
$details = [
"name" => "Lorem Graham",
"username" => "Lorem",
"email" => "lorem@march.biz",
"address" => [
"street" => "Chorus Light",
"suite" => "Apt. 556",
"city" => "Arizona",
"zipcode" => "92998-3874",
"geo" => [
"lat" => "-37.3159",
"lng" => "81.1496",
],
],
"phone" => "1-770-736-8031 x56442",
"website" => "loremipsum.org",
"company" => [
"name" => "Rodri-Corona",
"catchPhrase" => "Multi-layered client-server neural-net",
"bs" => "harness real-time e-markets",
]
];
$response = Http::post('https://jsonplaceholder.typicode.com/users', $details);
$user = $response->json();
dd($user);
Using Laravel's built-in HTTP client, we make an HTTP POST request to the URL 'https://jsonplaceholder.typicode.com/users', sending a JSON payload with the user details provided in the $details
array.
The Http::post()
method sends an HTTP POST request to the specified URL with the $details
array as the request body. The response from the server is then stored in the $response
variable.
The json()
method is called on the $response
object to retrieve the response body as a JSON object. The $user
variable is then set to this JSON object. Finally, the dd()
function is used to dump and die the $user
object to the browser for inspection.
PUT Request
use Illuminate\Support\Facades\Http;
$response = Http::put('https://jsonplaceholder.typicode.com/users/3', [
"name" => "Clementino Baucho",
"username" => "Clementino",
"email" => "clementino@baucho.net"
]);
$user = $response->json();
dd($user);
Using Laravel's built-in HTTP client, we make an HTTP PUT request to the URL 'https://jsonplaceholder.typicode.com/users/3', updating the user details for the user with ID 3.
The Http::put()
method sends an HTTP PUT request to the specified URL with the updated user details provided in the array as the request body. The response from the server is then stored in the $response
variable.
The json()
method is called on the $response
object to retrieve the response body as a JSON object. The $user
variable is then set to this JSON object. Finally, the dd()
function is used to dump and die the $user
object to the browser for inspection.
DELETE Request
use Illuminate\Support\Facades\Http;
$response = Http::delete('https://jsonplaceholder.typicode.com/posts/3');
if($response->status() == 200) {
dd('User deleted successfully!');
} else {
dd('Sorry, something went wrong.');
}
Using Laravel's built-in HTTP client, we make an HTTP DELETE request to the URL 'https://jsonplaceholder.typicode.com/posts/3', deleting the resource with ID 3.
The Http::delete()
method sends an HTTP DELETE request to the specified URL to delete the resource. The response from the server is then stored in the $response
variable.
The status()
method is called on the $response
object to retrieve the status code of the response. The if
statement checks if the status code is 200 (which indicates a successful response). If so, it displays the message "User deleted successfully!" using the dd()
function. Otherwise, the else
block executes, and the message "Sorry, something went wrong." is displayed using the dd()
function.
Conclusion
Making HTTP requests to external APIs is an important part of modern web development. Laravel's built-in HTTP client provides an easy and efficient way for you to make these requests. With just a few lines of code, you can send HTTP GET, POST, PUT, and DELETE requests. That enables you to receive responses from external APIs and work with the data these requests return in your Laravel applications.
This blog post covered the basics of making HTTP requests to external APIs using Laravel's Http facade. We explained how you can use this facade to send GET, POST, PUT, and DELETE requests and how you can handle the responses from these requests. We have also provided code examples to illustrate using the Http facade to interact with external APIs.
By leveraging Laravel's Http facade, you can easily incorporate external APIs into your Laravel applications, which can help you to save time and effort in implementing certain functionalities. This blog post has given you a good understanding of how to make HTTP requests to external APIs using Laravel and inspired you to explore further.