Search code examples
phphtmlsimple-html-dom

How can I create DOM object from a HTML file in php language?


I want to parsing data from homepage on this url. As you can see this url is HTML file and I read below:

// Create a DOM object from a HTML file
$html = file_get_html('test.htm');

so I just type a code below

include "simple_html_dom.php";
$html = file_get_html('eecs.kookmin.ac.kr/site/computer/notice.htm');
echo $html->plaintext;

The error message is:

Error message Warning: file_get_contents(eecs.kookmin.ac.kr/site/computer/notice.ht‌​m): failed to open stream: No such file or directory in C:\Bitnami\wampstack-5.6.27-0\apache2\htdocs\simple_html_dom‌​.php on line 76

what should I do?


Solution

  • You can get the HTML code using the Snoopy Class (https://sourceforge.net/projects/snoopy). Next code displays the HTML code inside of a <textarea> tag, then it displays the page itself, copy-paste next code in a PHP file and open it in your browser:

    <!DOCTYPE html>
    <html>
      <head>
        <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=euc-kr">
        <META HTTP-EQUIV="Content-language" CONTENT="ko">
      </head>
      <body>
    <?php
    require("Snoopy.class.php"); // ◄■■ GET SNOOPY FROM https://sourceforge.net/projects/snoopy
    $snoopy = new Snoopy;
    $snoopy->fetch("http://eecs.kookmin.ac.kr/site/computer/notice.htm");
    $html = mb_convert_encoding( $snoopy->results, "UTF-8", "EUC-KR" ); // ◄■■ GET HTML CODE.
    echo "<textarea rows='25' cols='80'>$html</textarea>"; // ◄■■ DISPLAY THE HTML.
    echo $html; // ◄■■ DISPLAY THE WEBPAGE.
    ?>
      </body>
    </html>
    

    The Snoopy Class is only one file, make sure the file is in the same directory your PHP file is.