Laravel 10 cURL HTTP Request Example

Laravel 10 cURL HTTP Request Example

Laravel 10 is the latest version of the popular PHP framework, which is widely used for building robust web applications. One of the most common tasks in web development is to make HTTP requests to external APIs, and Laravel provides a convenient way to do this using the cURL library. In this article, we will explore how to make cURL HTTP requests in Laravel 10 using some examples.

Introduction to cURL

cURL is a command-line tool and library that allows you to transfer data between servers using various protocols such as HTTP, HTTPS, FTP, SMTP, and more. It is widely used in web development to make HTTP requests to external APIs and fetch data.

Laravel 10 provides a convenient way to make HTTP requests using the cURL library. The built-in cURL library allows you to send HTTP requests and receive responses using various methods such as GET, POST, PUT, DELETE, and more.

Example 1: Making a GET Request

Let’s start with a simple example of making a GET request using cURL in Laravel 10. We will make a request to the JSONPlaceholder API to fetch a list of users.

use Illuminate\Support\Facades\Http;

$response = Http::withHeaders([
    'Accept' => 'application/json',
])
->get('https://jsonplaceholder.typicode.com/users');

$users = $response->json();

This code snippet uses the Http facade in Laravel 10 to make a GET request to the JSONPlaceholder API to fetch a list of users. The withHeaders method is used to set the Accept header to application/json, which tells the server that we expect a JSON response. The get method is used to make the GET request to the specified URL, and the response is stored in the $response variable.

The json method is then used to parse the response body as a JSON object and assign it to the $users variable. This makes it easy to work with the response data in your Laravel application.

Overall, this code snippet demonstrates how to use the Http facade in Laravel 10 to make HTTP requests and handle responses in your web applications. By using the built-in cURL library and the Http facade, you can easily integrate with external APIs and fetch data in your Laravel application.

In this example, we first import the Http facade and use it to make a GET request to the specified URL. We also set the Accept header to application/json, which tells the server that we expect a JSON response.

Once we receive the response, we can use the json() method to parse the response body as a JSON object and assign it to the $users variable.

Example 2: Making a POST Request

Next, let’s look at how to make a POST request using cURL in Laravel 10. We will create a new user on the JSONPlaceholder API by sending a POST request with the user data.

use Illuminate\Support\Facades\Http;

$response = Http::withHeaders([
    'Accept' => 'application/json',
])
->post('https://jsonplaceholder.typicode.com/users', [
    'name' => 'John Doe',
    'email' => '[email protected]',
    'phone' => '1234567890',
]);

$user = $response->json();

This code snippet uses the Http facade in Laravel 10 to make a POST request to the JSONPlaceholder API to create a new user. The withHeaders method is used to set the Accept header to application/json, which tells the server that we expect a JSON response. The post method is used to make the POST request to the specified URL, and the user data is passed as an array in the second parameter.

The response is stored in the $response variable, and the json method is used to parse the response body as a JSON object and assign it to the $user variable. This makes it easy to work with the response data in your Laravel application.

Overall, this code snippet demonstrates how to use the Http facade in Laravel 10 to make HTTP requests and handle responses in your web applications. By using the built-in cURL library and the Http facade, you can easily integrate with external APIs and create or modify data in your Laravel application.

In this example, we use the post() method to send a POST request to the specified URL with the user data as a JSON object. The response will contain the newly created user object, which we can parse using the json() method and assign it to the $user variable.

Example 3: Making a PUT Request

Next, let’s look at how to make a PUT request using cURL in Laravel 10. We will update an existing user on the JSONPlaceholder API by sending a PUT request with the updated user data.

use Illuminate\Support\Facades\Http;

$response = Http::withHeaders([
    'Accept' => 'application/json',
])
->put('https://jsonplaceholder.typicode.com/users/1', [
    'name' => 'John Doe',
    'email' => '[email protected]',
    'phone' => '1234567890',
]);

$user = $response->json();

This code snippet uses the Http facade in Laravel 10 to make a PUT request to the JSONPlaceholder API to update an existing user with the ID of 1. The withHeaders method is used to set the Accept header to application/json, which tells the server that we expect a JSON response. The put method is used to make the PUT request to the specified URL, and the updated user data is passed as an array in the second parameter.

The response is stored in the $response variable, and the json method is used to parse the response body as a JSON object and assign it to the $user variable. This makes it easy to work with the response data in your Laravel application.

Overall, this code snippet demonstrates how to use the Http facade in Laravel 10 to make HTTP requests and handle responses in your web applications. By using the built-in cURL library and the Http facade, you can easily integrate with external APIs and update existing data in your Laravel application.

In this example, we use the put() method to send a PUT request to the specified URL with the updated user data as a JSON object. The response will contain the updated user object, which we can parse using the json() method and assign it to the $user variable.

Example 4: Making a DELETE Request

Finally, let’s look at how to make a DELETE request using cURL in Laravel 10. We will delete an existing user on the JSONPlaceholder API

by sending a DELETE request to the user’s endpoint.

use Illuminate\Support\Facades\Http;

$response = Http::withHeaders([
    'Accept' => 'application/json',
])
->delete('https://jsonplaceholder.typicode.com/users/1');

$isDeleted = $response->status() === 200;

This code snippet uses the Http facade in Laravel 10 to make a DELETE request to the JSONPlaceholder API to delete an existing user with the ID of 1. The withHeaders method is used to set the Accept header to application/json, which tells the server that we expect a JSON response. The delete method is used to make the DELETE request to the specified URL.

The response is stored in the $response variable, and the status method is used to get the HTTP status code returned by the server. We then compare this status code to 200 to determine if the user was deleted successfully.

Overall, this code snippet demonstrates how to use the Http facade in Laravel 10 to make HTTP requests and handle responses in your web applications. By using the built-in cURL library and the Http facade, you can easily integrate with external APIs and delete existing data in your Laravel application.

In this example, we use the delete() method to send a DELETE request to the specified URL. The response will contain a status code indicating the success or failure of the request. In this case, we check if the status code is 200 to confirm that the user was deleted successfully.

Conclusion

In this article, we explored how to make cURL HTTP requests in Laravel 10 using some examples. We saw how to make GET, POST, PUT, and DELETE requests to external APIs and fetch data. By using the built-in Http facade in Laravel 10, you can easily make HTTP requests and handle responses in your web applications.

This Article Ideas has been taken from the following websites; which are following:

https://www.itsolutionstuff.com/post/laravel-http-curl-get-request-exampleexample.html

https://www.educative.io/answers/how-to-use-curl-in-laravel

https://stackoverflow.com/questions/48279382/curl-request-in-laravel

Laravel 10 cURL HTTP Request Example

https://www.nicesnippets.com/blog/laravel-8-http-curl-post-request-with-headers-example

https://websolutionstuff.com/post/laravel-8-curl-http-request-example

PHP Laravel CURL HTTP GET POST Request – Laravel

Comments

Leave a Reply

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