How can PHP developers ensure that their code complies with the Same-Origin Policy when making requests to external domains?

To comply with the Same-Origin Policy when making requests to external domains in PHP, developers can use the cURL library to set the appropriate headers in their HTTP requests. By setting the "Origin" header to the requesting domain, developers can ensure that the request complies with the Same-Origin Policy.

$url = 'https://example.com/api/data';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Origin: https://www.yourdomain.com'
));
$response = curl_exec($ch);
curl_close($ch);