To send a cURL request in PHP, you can use PHP’s built-in cURL functions. These functions allow you to interact with APIs or web services easily. Most PHP setups already include the cURL extension, so you don’t need to install anything extra. Here’s how it works:

- Start cURL: Use
curl_init()to start a new cURL session. - Set Options: Use
curl_setopt()to set various options, like the URL to send the request to, the type of request (GET, POST, etc.), and the data to send. - Execute the Request: Use
curl_exec()to send the request and get the response. - Check for Errors: Use
curl_errno()orcurl_error()to see if something went wrong. - Close cURL: Use
curl_close()to end the session and free up resources.
Important curl_setopt() Parameters
Here are the most commonly used cURL options:
| Option | Description | Example |
|---|---|---|
CURLOPT_URL | Sets the URL for the request. | curl_setopt($ch, CURLOPT_URL, "https://example.com"); |
CURLOPT_RETURNTRANSFER | Returns the response as a string instead of outputting it directly. | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
CURLOPT_POST | Indicates the request is a POST request. | curl_setopt($ch, CURLOPT_POST, true); |
CURLOPT_POSTFIELDS | Sends data in the body of the request. Works for POST, PUT, PATCH, etc. | curl_setopt($ch, CURLOPT_POSTFIELDS, "key1=value1&key2=value2"); or curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(['key' => 'value'])); |
CURLOPT_HTTPHEADER | Sets custom headers for the request. | curl_setopt($ch, CURLOPT_HTTPHEADER, ["Content-Type: application/json"]); |
CURLOPT_USERPWD | Sends credentials for Basic Authentication in the format username:password. | curl_setopt($ch, CURLOPT_USERPWD, "user:pass"); |
CURLOPT_TIMEOUT | Sets a timeout (in seconds) for the request. | curl_setopt($ch, CURLOPT_TIMEOUT, 30); |
CURLOPT_CUSTOMREQUEST | Specifies the request method (e.g., PUT, DELETE, PATCH) explicitly. | curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE"); |
CURLOPT_FOLLOWLOCATION | Follows redirects (if the server issues a 3xx redirect response). | curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); |
CURLOPT_SSL_VERIFYPEER | Verifies the server’s SSL certificate (use false for self-signed certificates, but not recommended in production). | curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); |
CURLOPT_SSL_VERIFYHOST | Verifies the hostname in the SSL certificate (set to 0 to disable, 2 to enforce). | curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); |
CURLOPT_HEADER | Includes the response headers in the output. | curl_setopt($ch, CURLOPT_HEADER, true); |
CURLOPT_COOKIE | Sends cookies with the request. | curl_setopt($ch, CURLOPT_COOKIE, "name=value; othername=othervalue"); |
CURLOPT_COOKIEFILE | Specifies a file to read cookies from. | curl_setopt($ch, CURLOPT_COOKIEFILE, "/path/to/cookie.txt"); |
CURLOPT_COOKIEJAR | Specifies a file to save cookies to after the request. | curl_setopt($ch, CURLOPT_COOKIEJAR, "/path/to/cookie.txt"); |
CURLOPT_USERAGENT | Sets a custom User-Agent string for the request. | curl_setopt($ch, CURLOPT_USERAGENT, "MyApp/1.0"); |
CURLOPT_REFERER | Sets the Referer header for the request. | curl_setopt($ch, CURLOPT_REFERER, "https://example.com"); |
CURLOPT_VERBOSE | Outputs detailed debugging information. | curl_setopt($ch, CURLOPT_VERBOSE, true); |
CURLOPT_HTTPAUTH | Sets the authentication method (e.g., CURLAUTH_BASIC, CURLAUTH_DIGEST). | curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); |
CURLOPT_PROXY | Specifies a proxy server to use for the request. | curl_setopt($ch, CURLOPT_PROXY, "http://proxyserver:8080"); |
CURLOPT_PROXYUSERPWD | Sends proxy authentication credentials in the format username:password. | curl_setopt($ch, CURLOPT_PROXYUSERPWD, "user:pass"); |
CURLOPT_ENCODING | Specifies the encoding to use for the request (e.g., gzip, deflate). | curl_setopt($ch, CURLOPT_ENCODING, "gzip"); |
CURLOPT_MAXREDIRS | Sets the maximum number of redirects to follow. | curl_setopt($ch, CURLOPT_MAXREDIRS, 10); |
<?php
// Initialize cURL session
$ch = curl_init();
// Set the URL
curl_setopt($ch, CURLOPT_URL, "https://example.com/api");
// Set HTTP method
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
// Add POST data
$data = json_encode([
"name" => "John",
"email" => "john@example.com"
]);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
// Set headers
curl_setopt($ch, CURLOPT_HTTPHEADER, [
"Content-Type: application/json",
"Authorization: Bearer YOUR_API_TOKEN"
]);
// Enable response return
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Set timeout
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
// Follow redirects
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
// SSL verification
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
// Execute the request
$response = curl_exec($ch);
// Check for errors
if (curl_errno($ch)) {
echo "Error: " . curl_error($ch);
} else {
echo "Response: " . $response;
}
// Close the cURL session
curl_close($ch);
?>


