Home >Backend Development >PHP Tutorial >Detailed introduction to PHP collection class snoopy (snoopy usage tutorial)_PHP tutorial

Detailed introduction to PHP collection class snoopy (snoopy usage tutorial)_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:24:37830browse

Snoopy is a php class that is used to simulate the functions of a browser. It can obtain web content, send forms, and can be used to develop some collection programs and thief programs. This article introduces the usage tutorial of snoopy in detail.

Some features of Snoopy:
Fetch the content of the web page fetch
Fetch the text content of the web page (remove HTML tags) fetchtext
Fetch web links, form fetchlinks fetchform
Support proxy host
Supports basic username/password authentication
Support setting user_agent, referer (source), cookies and header content (header file)
Supports browser redirection and can control redirection depth
Can expand links in web pages into high-quality URLs (default)
Submit data and get return value
Support tracking HTML framework
Support passing cookies when redirecting
It only requires PHP 4 or above. Since it is a PHP class, there is no need to expand the support. The best choice when the server does not support curl,

Snoopy class methods and examples:

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.
The fetched 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.


Snoopy collection class attributes: (default value is in brackets)

$host Connected host
$port Connected port
$proxy_host The proxy host to use, if any
$proxy_port The proxy host port to use, if any
$agent User Agent Disguise (Snoopy v0.1)
$referer Origin 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 type (image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, */*)
$error Where to report the error, if any
$response_code The response code returned from the server
$headers Headers returned from the server
$maxlength Maximum return 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 Status of the captured http
$temp_dir Temporary file directory (/tmp) that the web server can write to
$curl_path The directory of cURL binary, if there is no cURL binary, set it to false

Here is an example:

Copy code The code is as follows:

include "Snoopy.class.php";
 $snoopy = new Snoopy;
 
 $snoopy->proxy_host = "http://www.jb51.net";
 $snoopy->proxy_port = "80";
 
 $snoopy->agent = "(compatible; MSIE 4.01; MSN 2.5; AOL 4.0; Windows 98)";
 $snoopy->referer = "http://www.jb51.net";
 
 $snoopy->cookies["SessionID"] = 238472834723489l;
 $snoopy->cookies["favoriteColor"] = "RED";
 
 $snoopy->rawheaders["Pragma"] = "no-cache";
 
 $snoopy->maxredirs = 2;
 $snoopy->offsiteok = false;
 $snoopy->expandlinks = false;
 
 $snoopy->user = "joe";
 $snoopy->pass = "bloe";
 
 if($snoopy->fetchtext("http://www.jb51.net"))
 {
 echo "
".htmlspecialchars($snoopy->results)."
n";
 }
 else
 echo "error fetching document: ".$snoopy->error."n";

获取指定url内容

复制代码 代码如下:
 $url = "http://www.jb51.net";
 include("snoopy.php");
 $snoopy = new Snoopy;
 $snoopy->fetch($url); //获取所有内容
 echo $snoopy->results; //显示结果
 //可选以下
 $snoopy->fetchtext //获取文本内容(去掉html代码)
 $snoopy->fetchlinks //获取链接
 $snoopy->fetchform  //获取表单
 ?>

表单提交

复制代码 代码如下:
$formvars["username"] = "admin";
$formvars["pwd"] = "admin";
$action = "http://www.jb51.net";//表单提交地址
$snoopy->submit($action,$formvars);//$formvars为提交的数组
echo $snoopy->results; //获取表单提交后的 返回的结果
//可选以下
$snoopy->submittext; //提交后只返回 去除html的 文本
$snoopy->submitlinks;//提交后只返回 链接
?>

既然已经提交的表单 那就可以做很多事情 接下来我们来伪装ip,伪装浏览器

伪装浏览器

复制代码 代码如下:
$formvars["username"] = "lanfengye";
$formvars["pwd"] = "lanfengye";
$action = "http://www.jb51.net";
include "snoopy.php";
$snoopy = new Snoopy;
$snoopy->cookies["PHPSESSID"] = 'fc106b1918bd522cc863f36890e6fff7'; //伪装sessionid
$snoopy->agent = "(compatible; MSIE 4.01; MSN 2.5; AOL 4.0; Windows 98)"; //伪装浏览器
$snoopy->referer = "http://www.jb51.net"; //伪装来源页地址 http_referer
$snoopy->rawheaders["Pragma"] = "no-cache"; //cache 的http头信息
$snoopy->rawheaders["X_FORWARDED_FOR"] = "127.0.0.101"; //伪装ip
$snoopy->submit($action,$formvars);
echo $snoopy->results;
?>

It turns out that we can camouflage session, camouflage browser, camouflage IP, haha ​​we can do a lot of things.
For example, if you bring a verification code and verify your IP to vote, you can vote non-stop.
ps: Disguising the IP here is actually disguising the http header, so the IP obtained through REMOTE_ADDR cannot be disguised,
On the contrary, those who obtain IP through http header (the kind that can prevent proxy) can create their own IP.
Regarding how to verify the code, let’s briefly talk:
First, use an ordinary browser to view the page and find the sessionid corresponding to the verification code,
Also note down the sessionid and verification code values,
Next, use snoopy to fake it.
Principle: Since it is the same sessionid, the verification code obtained is the same as the one entered for the first time.

Sometimes we may need to fake more things, snoopy completely thought of it for us

$snoopy->proxy_host = "http://www.jb51.net";
$snoopy->proxy_port = "8080"; //Use proxy
$snoopy->maxredirs = 2; //Number of redirects
$snoopy->expandlinks = true; //Whether to complete the link is often used during collection
// For example, the link is /images/taoav.gif, which can be changed to its full linkhttp://www.jb51. net/images/taoav.gif
$snoopy->maxframes = 5 //Maximum number of frames allowed
//Note that when grabbing the frame, $snoopy->results returns an array
$snoopy->error //Return error message
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/825391.htmlTechArticleSnoopy is a php class that is used to simulate the functions of a browser. It can obtain web content and send forms. It can be used To develop some collection programs and thief programs, this article introduces snoopy in detail...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn