What best practices should be followed when sending XML data in a cURL HTTP POST request in PHP?

When sending XML data in a cURL HTTP POST request in PHP, it is important to set the appropriate headers, specify the XML data in the request body, and handle any errors that may occur during the request. Additionally, it is recommended to properly escape the XML data to prevent any potential security vulnerabilities.

$url = 'https://example.com/api';
$xml_data = '<data><item>value</item></data>';

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_data);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml'));
$response = curl_exec($ch);

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

curl_close($ch);