In what ways can PHP be utilized to open and read websites for data extraction purposes?
To open and read websites for data extraction purposes using PHP, you can utilize the cURL library. cURL allows you to make HTTP requests to a website, retrieve the HTML content, and then parse the data as needed for extraction.
<?php
// Initialize cURL session
$ch = curl_init();
// Set the URL to be fetched
curl_setopt($ch, CURLOPT_URL, 'https://www.example.com');
// Set cURL options to return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Execute the cURL session
$result = curl_exec($ch);
// Close cURL session
curl_close($ch);
// Output the fetched HTML content
echo $result;
?>