Home > Article > Backend Development > PHP collection class Snoopy.class.php introduction and download_PHP tutorial
Snoopy is a very powerful PHP class that can be used to simulate a browser to obtain web content and send forms task. The following is a detailed introduction to the features and some common uses of Snoopy.class.php.
Official website: http://snoopy.sourceforge.net/ (If you cannot open foreign websites, you can use Tianxing Browser)
Download address: http://sourceforge.net/projects/snoopy/
Download address of this site: PHP collection library Snoopy.class.php download
Here are some features of Snoopy:
1. Fetch the content of the web page fetch
2. Fetch the text content of the web page (remove HTML tags) fetchtext
3. Fetch web page links, form fetchlinks fetchform
4. Support proxy host
5. Support basic username/password verification
6. Support setting user_agent, referer (source), cookies and header content (header file)
7. Supports browser redirection and can control redirection depth
8. Can expand links in web pages into high-quality URLs (default)
9. Submit data and get the return value
10. Support tracking HTML framework (added in v0.92)
11. Support passing cookies when redirecting
Note: Using Snoopy.class.php requires PHP 4 or above. Since it is a PHP class, there is no need to expand support. It is the best choice when the server does not support curl.
The following is an introduction to some commonly used class methods:
fetch($URI)
This is the method used to crawl the content of web pages. The $URI parameter is the URL address of the crawled web page, and the crawled results are stored in $this->results. If you are scraping a frame, Snoopy will track each frame and store it in an array, then store it in $this->results.
fetchtext($URI)
This method is similar to fetch(). The only difference is that this method will remove HTML tags and other irrelevant data and only return the text content in the web page.
fetchform($URI)
This method is similar to fetch(). The only difference is that this method will remove HTML tags and other irrelevant data, and only return the form content (form) in the web page.
fetchlinks($URI)
This method is similar to fetch(). The only difference is that this method will remove HTML tags and other irrelevant data, and only return links in the web page. By default, relative links will be automatically completed and converted into full URLs.
submit($URI,$formvars)
This method sends a confirmation form to the link address specified by $URL. $formvars is an array that stores form parameters.
submittext($URI,$formvars)
This method is similar to submit(). The only difference is that this method will remove HTML tags and other irrelevant data, and only return the text content of the web page after login.
submitlinks($URI)
This method is similar to submit(). The only difference is that this method will remove HTML tags and other irrelevant data and only return the link in the web page. By default, relative links will be automatically completed and converted into full URLs.
Class attributes: (Default values are in brackets)
$host the connected host
$port The port to connect to
$proxy_host The proxy host to use, if any
$proxy_port The proxy host port to use, if any
$agent User agent masquerading (Snoopy v0.1)
$referer source information, if available
$cookies cookies, if any
$rawheaders Other header information, if any
$maxredirs Maximum number of redirects, 0=not allowed (5)
$offsiteok whether or not to allow redirects off-site. (true)
$expandlinks Whether to complete all links to complete addresses (true)
$user authentication username, if available
$pass authentication username, if available
$accept http accept types (image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, */*)
$error Where is the error reported, if any
$response_code Response code returned from the server
$headers Headers returned from the server
$maxlength The longest returned data length
$read_timeout read operation timeout (requires PHP 4 Beta 4+) set to 0 for no timeout
$timed_out If a read operation times out, this attribute returns true (requires PHP 4 Beta 4+)
$maxframes Maximum number of frames allowed to be tracked
$status The status of the captured http
$temp_dir The temporary file directory that the web server can write to (/tmp)
$curl_path cURL binary directory, if there is no cURL binary, set it to false
The following are some common usage examples:
(1) Capture the text on the home page of the PHP programmer’s notes website
<?php include 'Snoopy.class.php'; $snoopy=new Snoopy; $snoopy->fetchtext("http://www.phpernote.com"); echo $snoopy->results;
(2) Grab all the links on the home page of the PHP programmer’s notes website
<?php include 'Snoopy.class.php'; $snoopy=new Snoopy; $snoopy->fetchlinks("http://www.phpernote.com"); print_r($snoopy->results);
(3) Get what fields need to be sent to log in to Renren and what is the target address
<?php include 'Snoopy.class.php'; $snoopy=new Snoopy; $snoopy->fetchform("http://www.renren.com/PLogin.do"); print_r($snoopy->results);
(4) Simulate login to Renren
<?php set_time_limit(0); require "Snoopy.class.php"; $snoopy=new Snoopy(); $snoopy->referer='http://www.renren.com/'; $snoopy->agent="Mozilla/5.0 (Windows NT 6.1; rv:22.0) Gecko/20100101 Firefox/22.0"; $submit_vars['email'] ='登陆账号'; $submit_vars['password'] ='登陆密码'; $url='http://www.renren.com/PLogin.do';//登陆数据提交的URL地址 $snoopy->submit($url,$submit_vars); $snoopy->fetch("http://www.renren.com/");//希望获取的页面数据 echo $snoopy->results;//www.phpernote.com