'How to use snoopy class in PHP?

I'm beginner of php, I'm making simple program, and that use some crawling web site (not private information). The result that I expected is HTML CODE, like a

<html><head><title>blabla blabla</title></head>...................

But I checked the result, the screen shown up. not a raw code, for example,

include "Snoopy.class.php";
$snoopy = new Snoopy;

$snoopy->fetch("http://stackoverflow.com/");
echo $snoopy->results;

How to I get information to HTML Code? And Do you have another good parsing library in PHP? (like a beautifulsoup on Python, and Jsoup on Java)

** The result of above code : not a html code, but screen **

not html source, but screen.



Solution 1:[1]

To see the source code using your browser instead of it rendering the HTML your last line should be:

echo htmlspecialchars($snoopy->results);

Solution 2:[2]

It's very simple

     // Add snoopy class and initiate it
     require "snoopy/Snoopy.class.php";
     $snoopy = new Snoopy;         

    // THis fetches the html
    $snoopy->fetch("http://www.php.net/");
                  $text = $snoopy->results;
    // This fetches the text with html tags stripped
    $snoopy->fetchtext("http://www.php.net/");
                  $text = $snoopy->results;
    // This fetches all the links
    $snoopy->fetchlinks('http://www.php.net/');
    $linksarray = $snoopy->results;

Snoopy works great for me. So hope that helps

Solution 3:[3]

If you want to fetch the html from the URL you can do this simple do this by file_get_contents function of php.

$url = 'http://stackoverflow.com/';
$html = file_get_contents($url);
// echo $url -> wrong
echo $html;

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Dean Jenkins
Solution 2 Alex
Solution 3 Ariel