Laravel 10 cURL HTTP Request Example

Curl is a powerful command-line tool that allows you to transfer data to and from servers using various protocols such as HTTP, FTP, SMTP, and many others. In Laravel, Curl can be used to send HTTP requests to external APIs and retrieve responses. In this tutorial, you will learn how to use Curl in Laravel and some of its applications.

How to use cURL HTTP Requests in Laravel

With the following techniques, you can easily integrate external APIs with HTTP GET & POST CURL requests into your Laravel projects and build more powerful applications.

  • Installing Curl in Laravel
  • Sending GET requests using Curl
  • Sending POST requests using Curl

Installing Curl in Laravel

Before you start using Curl in Laravel, we need to ensure that it is installed in our system. Most of the Linux distributions come with Curl pre-installed. However, if you are using Windows, you need to download and install it separately.

To install Curl in Laravel, we can use the following command:

sudo apt-get install curl

Sending GET requests using Curl

To send a GET request using Curl in Laravel, you need to use the curl_init() function to initialize a new Curl session. Then, you can set the URL of the API endpoint we want to access using the CURLOPT_URL option. Finally, And can execute the Curl session using the curl_exec() function and close the session using the curl_close() function.

Here is an example of sending a GET request using Curl in Laravel:

$url = "https://jsonplaceholder.typicode.com/posts";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
$data = json_decode($response, true);

Let’s break down the above code step-by-step:

  1. $url = "https://jsonplaceholder.typicode.com/posts"; This line initializes a variable $url and assigns the URL of the API endpoint that we want to access. In this case, we are using a demo API that returns a list of posts.
  2. $ch = curl_init(); This line initializes a new Curl session using the curl_init() function and assigns it to the variable $ch.
  3. curl_setopt($ch, CURLOPT_URL, $url); This line sets the URL of the request to the value of the $url variable using the curl_setopt() function.
  4. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); This line sets the CURLOPT_RETURNTRANSFER option to true, which instructs Curl to return the response as a string instead of outputting it directly.
  5. $response = curl_exec($ch); This line executes the Curl session using the curl_exec() function and assigns the response to the variable $response.
  6. curl_close($ch); This line closes the Curl session using the curl_close() function.
  7. $data = json_decode($response, true); This line decodes the JSON response string returned by the API endpoint into a PHP array using the json_decode() function and assigns it to the variable $data.

Sending POST requests using Curl

To send a POST request using Curl in Laravel, you need to define the data want to send in the POST request as an associative array. Youcan then initialize a new Curl session using the curl_init() function and set the CURLOPT_URL option to the URL of the API endpoint want to access.

Here is an example of sending a POST request using Curl in Laravel:

$url = "https://jsonplaceholder.typicode.com/posts";
$data = array(
    'title' => 'foo',
    'body' => 'bar',
    'userId' => 1
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
$data = json_decode($response, true);

Let’s break down the above given code snippet line by line:

  1. $url = "https://jsonplaceholder.typicode.com/posts";
    • This line assigns a string value containing the URL of the API endpoint we want to access to the $url variable.
  2. $data = array( 'title' => 'foo', 'body' => 'bar', 'userId' => 1 );
    • This line defines an associative array containing the data we want to send in the POST request. The array contains three key-value pairs: 'title' => 'foo', 'body' => 'bar', and 'userId' => 1.
  3. $ch = curl_init();
    • This line initializes a new Curl session and assigns the resulting handle to the $ch variable.
  4. curl_setopt($ch, CURLOPT_URL, $url);
    • This line sets the CURLOPT_URL option of the Curl session to the URL of the API endpoint we want to access.
  5. curl_setopt($ch, CURLOPT_POST, true);
    • This line sets the CURLOPT_POST option of the Curl session to true, indicating that we want to send a POST request.
  6. curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
    • This line sets the CURLOPT_POSTFIELDS option of the Curl session to the data we want to send in the POST request. The http_build_query() function converts the associative array into a URL-encoded string that can be sent as the request body.
  7. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    • This line sets the CURLOPT_RETURNTRANSFER option of the Curl session to true, indicating that we want Curl to return the response as a string instead of outputting it directly.
  8. $response = curl_exec($ch);
    • This line executes the Curl session and assigns the response string to the $response variable.
  9. curl_close($ch);
    • This line closes the Curl session.
  10. $data = json_decode($response, true);
  • This line decodes the response string as a JSON-encoded string and converts it into a PHP associative array. The resulting array is assigned to the $data variable.

Conclusion

Curl is a powerful tool that can be used to send HTTP requests to external APIs in Laravel. In this tutorial, you learned how to use Curl in Laravel to send GET and POST requests and retrieve responses. With these techniques, you can easily integrate external APIs into your Laravel projects and build more powerful applications.

Recommended Laravel Tutorials

AuthorDevendra Dode

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

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