How can one send a POST request using cURL in PHP?

To send a POST request using cURL in PHP, you need to set the CURLOPT_POST option to true and provide the data to be sent in the CURLOPT_POSTFIELDS option. Additionally, you may need to set other options such as the URL to send the request to. Here is a PHP code snippet that demonstrates how to send a POST request using cURL:

$url = 'https://example.com/api';
$data = array(
    'key1' => 'value1',
    'key2' => 'value2'
);

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$response = curl_exec($ch);

if($response === false){
    echo 'cURL error: ' . curl_error($ch);
} else {
    echo 'Response: ' . $response;
}

curl_close($ch);