What are common methods for extracting source code from external websites in PHP?
When extracting source code from external websites in PHP, common methods include using cURL to fetch the webpage's content, parsing the HTML using libraries like Simple HTML DOM Parser or PHP's built-in DOMDocument class, and then extracting the desired source code using regular expressions or XPath queries.
// Example code using cURL to fetch the webpage's content
$url = 'https://www.example.com';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$html = curl_exec($ch);
curl_close($ch);
// Example code using Simple HTML DOM Parser to extract source code
include('simple_html_dom.php');
$dom = str_get_html($html);
$sourceCode = $dom->find('pre#source-code', 0)->plaintext;
// Example code using DOMDocument to extract source code
$doc = new DOMDocument();
$doc->loadHTML($html);
$xpath = new DOMXPath($doc);
$sourceCode = $xpath->query('//pre[@id="source-code"]')->item(0)->nodeValue;