What are the best practices for handling web scraping in PHP, especially when it comes to obtaining permission from website owners?

When handling web scraping in PHP, it is important to obtain permission from website owners to avoid legal issues. One way to do this is by checking the website's terms of service or contacting the website owner directly to request permission. Additionally, it is good practice to include a user-agent header in your scraping script to identify yourself and provide contact information in case the website owner needs to reach out.

<?php
// Set the user-agent header to identify yourself
$opts = [
    'http' => [
        'user_agent' => 'Your Name (your@email.com)',
    ]
];

$context = stream_context_create($opts);

// Use file_get_contents with the created context
$data = file_get_contents('http://example.com', false, $context);

// Process the scraped data
echo $data;
?>