How to Send Curl Request ?

shivv89

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:

  1. Start cURL: Use curl_init() to start a new cURL session.
  2. 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.
  3. Execute the Request: Use curl_exec() to send the request and get the response.
  4. Check for Errors: Use curl_errno() or curl_error() to see if something went wrong.
  5. 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:

OptionDescriptionExample
CURLOPT_URLSets the URL for the request.curl_setopt($ch, CURLOPT_URL, "https://example.com");
CURLOPT_RETURNTRANSFERReturns the response as a string instead of outputting it directly.curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
CURLOPT_POSTIndicates the request is a POST request.curl_setopt($ch, CURLOPT_POST, true);
CURLOPT_POSTFIELDSSends 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_HTTPHEADERSets custom headers for the request.curl_setopt($ch, CURLOPT_HTTPHEADER, ["Content-Type: application/json"]);
CURLOPT_USERPWDSends credentials for Basic Authentication in the format username:password.curl_setopt($ch, CURLOPT_USERPWD, "user:pass");
CURLOPT_TIMEOUTSets a timeout (in seconds) for the request.curl_setopt($ch, CURLOPT_TIMEOUT, 30);
CURLOPT_CUSTOMREQUESTSpecifies the request method (e.g., PUT, DELETE, PATCH) explicitly.curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
CURLOPT_FOLLOWLOCATIONFollows redirects (if the server issues a 3xx redirect response).curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
CURLOPT_SSL_VERIFYPEERVerifies 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_VERIFYHOSTVerifies the hostname in the SSL certificate (set to 0 to disable, 2 to enforce).curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
CURLOPT_HEADERIncludes the response headers in the output.curl_setopt($ch, CURLOPT_HEADER, true);
CURLOPT_COOKIESends cookies with the request.curl_setopt($ch, CURLOPT_COOKIE, "name=value; othername=othervalue");
CURLOPT_COOKIEFILESpecifies a file to read cookies from.curl_setopt($ch, CURLOPT_COOKIEFILE, "/path/to/cookie.txt");
CURLOPT_COOKIEJARSpecifies a file to save cookies to after the request.curl_setopt($ch, CURLOPT_COOKIEJAR, "/path/to/cookie.txt");
CURLOPT_USERAGENTSets a custom User-Agent string for the request.curl_setopt($ch, CURLOPT_USERAGENT, "MyApp/1.0");
CURLOPT_REFERERSets the Referer header for the request.curl_setopt($ch, CURLOPT_REFERER, "https://example.com");
CURLOPT_VERBOSEOutputs detailed debugging information.curl_setopt($ch, CURLOPT_VERBOSE, true);
CURLOPT_HTTPAUTHSets the authentication method (e.g., CURLAUTH_BASIC, CURLAUTH_DIGEST).curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
CURLOPT_PROXYSpecifies a proxy server to use for the request.curl_setopt($ch, CURLOPT_PROXY, "http://proxyserver:8080");
CURLOPT_PROXYUSERPWDSends proxy authentication credentials in the format username:password.curl_setopt($ch, CURLOPT_PROXYUSERPWD, "user:pass");
CURLOPT_ENCODINGSpecifies the encoding to use for the request (e.g., gzip, deflate).curl_setopt($ch, CURLOPT_ENCODING, "gzip");
CURLOPT_MAXREDIRSSets 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);
?>

Leave a Comment