How can headers be modified and sent in PHP when extracting content from external websites to improve the scraping process?
When extracting content from external websites in PHP, modifying and sending headers can help improve the scraping process by mimicking a real user agent and avoiding being blocked by the server. This can be achieved by setting custom headers using the `stream_context_create()` function in PHP.
$url = 'https://example.com';
$options = array(
'http' => array(
'header' => "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3\r\n"
)
);
$context = stream_context_create($options);
$content = file_get_contents($url, false, $context);
echo $content;