How can data be extracted from a webpage using PHP?

To extract data from a webpage using PHP, you can use the file_get_contents() function to retrieve the HTML content of the webpage. Then, you can use DOMDocument and DOMXPath to parse the HTML content and extract the specific data you need based on HTML tags or attributes.

// URL of the webpage to extract data from
$url = 'https://example.com';

// Get the HTML content of the webpage
$html = file_get_contents($url);

// Create a new DOMDocument object
$dom = new DOMDocument();

// Load the HTML content into the DOMDocument
$dom->loadHTML($html);

// Create a new DOMXPath object
$xpath = new DOMXPath($dom);

// Extract data using XPath queries
$data = $xpath->query('//div[@class="content"]')->item(0)->nodeValue;

// Output the extracted data
echo $data;