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:
$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.$ch = curl_init();
This line initializes a new Curl session using thecurl_init()
function and assigns it to the variable$ch
.curl_setopt($ch, CURLOPT_URL, $url);
This line sets the URL of the request to the value of the$url
variable using thecurl_setopt()
function.curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
This line sets theCURLOPT_RETURNTRANSFER
option totrue
, which instructs Curl to return the response as a string instead of outputting it directly.$response = curl_exec($ch);
This line executes the Curl session using thecurl_exec()
function and assigns the response to the variable$response
.curl_close($ch);
This line closes the Curl session using thecurl_close()
function.$data = json_decode($response, true);
This line decodes the JSON response string returned by the API endpoint into a PHP array using thejson_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:
$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.
- This line assigns a string value containing the URL of the API endpoint we want to access to the
$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
.
- This line defines an associative array containing the data we want to send in the POST request. The array contains three key-value pairs:
$ch = curl_init();
- This line initializes a new Curl session and assigns the resulting handle to the
$ch
variable.
- This line initializes a new Curl session and assigns the resulting handle to the
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.
- This line sets the
curl_setopt($ch, CURLOPT_POST, true);
- This line sets the
CURLOPT_POST
option of the Curl session totrue
, indicating that we want to send a POST request.
- This line sets the
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. Thehttp_build_query()
function converts the associative array into a URL-encoded string that can be sent as the request body.
- This line sets the
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- This line sets the
CURLOPT_RETURNTRANSFER
option of the Curl session totrue
, indicating that we want Curl to return the response as a string instead of outputting it directly.
- This line sets the
$response = curl_exec($ch);
- This line executes the Curl session and assigns the response string to the
$response
variable.
- This line executes the Curl session and assigns the response string to the
curl_close($ch);
- This line closes the Curl session.
$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.