What are the advantages of using cURL for handling HTTP requests in PHP?

When handling HTTP requests in PHP, using cURL can provide several advantages such as better performance, support for various protocols (HTTP, HTTPS, FTP, etc.), the ability to set custom headers and options, and handling of cookies and sessions seamlessly.

// Initialize cURL session
$ch = curl_init();

// Set cURL options
curl_setopt($ch, CURLOPT_URL, 'http://example.com/api');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

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

// Close cURL session
curl_close($ch);

// Process the response data
echo $response;