What are common issues when making a POST XML request in PHP and how can they be resolved?

Issue: Common issues when making a POST XML request in PHP include incorrect headers, malformed XML data, and server-side parsing errors. To resolve these issues, ensure that the correct Content-Type header is set, validate the XML data before sending it, and handle any parsing errors gracefully.

// Fix for setting the correct Content-Type header
$url = 'https://example.com/api';
$xml_data = '<data>example</data>';

$headers = array(
    'Content-Type: application/xml',
);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $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, $headers);

$response = curl_exec($ch);
curl_close($ch);

echo $response;