How to send request using curl in PHP

In this article let us understand how to send request using curl in PHP. We will understand get and post request here.

Curl POST request

To make a POST request using cURL in PHP, you need to set additional options to specify the request method, data to be sent, and any required headers. Here’s an example of making a POST request using cURL:

// Initialize cURL
$ch = curl_init();

// Set the URL to post data to
$url = "https://example.com/api";

// Set POST data
$data = array(
    'name' => 'Coding Solutions Point,
    'email' => 'codingsolutionspoint@example.com'
);

// Set cURL options
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));

// Execute the request and get the response
$response = curl_exec($ch);

// Check for cURL errors
if (curl_errno($ch)) {
    $error = curl_error($ch);
    // Handle the error
} else {
    // Process the response
    echo $response;
}

// Close cURL
curl_close($ch);

In this example, we start by initializing a cURL session using curl_init().

We set the URL to which the POST request will be sent using curl_setopt() with the CURLOPT_URL option.

We create an array $data containing the POST parameters. In this example, we have ‘name’ and ’email’ fields.

We set the CURLOPT_POST option to true to indicate that we want to make a POST request.

We use http_build_query() to convert the $data array into a URL-encoded string suitable for POST data.

Then, we set the CURLOPT_POSTFIELDS option with the URL-encoded data.

Next, we execute the request using curl_exec(), which sends the HTTP request and returns the response.

We can check for any errors using curl_errno() and retrieve the error message using curl_error().

If there are no errors, we can process the response as needed. In this example, we simply echo the response.

Finally, we close the cURL session using curl_close().

Remember to adjust the URL, data, and any other options according to your specific needs.

Curl GET request

To make a GET request using cURL in PHP, you can use the CURLOPT_HTTPGET option to explicitly set the request method as GET. Here’s an example:

// Initialize cURL
$ch = curl_init();

// Set the URL to fetch
$url = "https://example.com/api?param1=value1&param2=value2";

// Set cURL options
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPGET, true);

// Execute the request and get the response
$response = curl_exec($ch);

// Check for cURL errors
if (curl_errno($ch)) {
    $error = curl_error($ch);
    // Handle the error
} else {
    // Process the response
    echo $response;
}

// Close cURL
curl_close($ch);

In this example, we initialize a cURL session using curl_init().

We set the URL to fetch by using curl_setopt() with the CURLOPT_URL option. You can specify any valid URL here. Additionally, you can append query parameters to the URL to include any necessary data.

We set CURLOPT_RETURNTRANSFER to true to instruct cURL to return the response as a string instead of directly outputting it.

We explicitly set CURLOPT_HTTPGET to true to specify that we want to make a GET request.

Next, we execute the request using curl_exec(), which sends the HTTP request and returns the response.

After executing the request, we can check for any errors using curl_errno() and retrieve the error message using curl_error().

If there are no errors, we can process the response as needed. In this example, we simply echo the response.

Finally, we close the cURL session using curl_close().

Remember to adjust the URL and any other options according to your specific needs.

That’s how we send request using curl in PHP.