===== HTML Seite einlesen =====
Mit dieser Function kann man (externe) HTML Seiten einlesen
//function to get the remote data
function url_get_contents ($url) {
if (function_exists('curl_exec')){
$conn = curl_init($url);
curl_setopt($conn, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($conn, CURLOPT_FRESH_CONNECT, true);
curl_setopt($conn, CURLOPT_RETURNTRANSFER, 1);
$url_get_contents_data = (curl_exec($conn));
curl_close($conn);
}elseif(function_exists('file_get_contents')){
$url_get_contents_data = file_get_contents($url);
}elseif(function_exists('fopen') && function_exists('stream_get_contents')){
$handle = fopen ($url, "r");
$url_get_contents_data = stream_get_contents($handle);
}else{
$url_get_contents_data = false;
}
return $url_get_contents_data;
}
So sieht es dann in Aktion aus:
$data = url_get_contents("http://www.google.com");
if($data){
//Do Something....
}
eingelesenen HTML Quelltext anzeigen lassen:
echo "".htmlentities($data)."
";
====Das ganze kann man auch mit DOMDocument und XPATH erledigen ====
loadHTMLFile($file);
$xpath = new DOMXpath($doc);
// example 1: for everything with an id
//$elements = $xpath->query("//*[@id]");
// example 2: for node data in a selected id
//$elements = $xpath->query("/html/body/div[@id='yourTagIdHere']");
// example 3: same as above with wildcard
$elements = $xpath->query("*/div[@id='yourTagIdHere']");
if (!is_null($elements)) {
foreach ($elements as $element) {
echo "
[". $element->nodeName. "]";
$nodes = $element->childNodes;
foreach ($nodes as $node) {
echo $node->nodeValue. "\n";
}
}
}
?>
Hier noch mal ein Link zu einer Diskussion über das Thema:\\
https://www.php.de/forum/webentwicklung/php-einsteiger/1560550-wert-von-bis-innerhalb-eines-tags-aus-einer-textdatei-auslesen