欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  php教程

php使用Snoopy.class.php做http get post请求

程序员文章站 2022-04-01 12:05:42
...

snoopy是一个php类,用来模仿web浏览器的功能,它能完成获取网页内容和发送表单的任务。

下面是它的一些特征:

1. 方便抓取网页的内容
2. 方便抓取网页的文字(去掉HTML代码)
3. 方便抓取网页的链接
4. 支持代理主机
5. 支持基本的用户/密码认证模式
6. 支持自定义用户agent,referer,cookies和header内容
7. 支持浏览器转向,并能控制转向深度
8. 能把网页中的链接扩展成高质量的url(默认)
9. 方便提交数据并且获取返回值
10.支持跟踪HTML框架(v0.92增加)
11.支持再转向的时候传递cookies

php使用Snoopy.class.php做http get post请求

1. [代码]通过get方法获取指定url内容

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

2. [代码]表单提交

<?php 
$formvars["username"] = "admin"; 
$formvars["pwd"] = "admin"; 

$action = "http://www.taoav.com";//表单提交地址 
$snoopy->submit($action,$formvars);//$formvars为提交的数组 
echo $snoopy->results; //获取表单提交后的 返回的结果 
//可选以下 
$snoopy->submittext; //提交后只返回 去除html的 文本 
$snoopy->submitlinks;//提交后只返回 链接 
?> 


伪装ip,伪装浏览器
<?php 
$formvars["username"] = "admin"; 
$formvars["pwd"] = "admin"; 
$action = "http://www.taoav.com"; 
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.php.cn/; //伪装来源页地址 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; 
?> 



<? 
include "Snoopy.class.php"; 
$snoopy = new Snoopy; 
$submit_url = "http://www.phpx.com/happy/logging.php?action=login"; 

$submit_vars["loginmode"] = "normal"; 
$submit_vars["styleid"] = "1"; 
$submit_vars["cookietime"] = "315360000"; 
$submit_vars["loginfield"] = "username"; 
$submit_vars["username"] = "********"; //你的用户名 
$submit_vars["password"] = "*******"; //你的密码 
$submit_vars["questionid"] = "0"; 
$submit_vars["answer"] = ""; 
$submit_vars["loginsubmit"] = "提 &nbsp; 交"; 
$snoopy->submit($submit_url,$submit_vars); 
print $snoopy->results;

?>

3. [代码]使用代理

<?php
include("snoopy.class.php");
$snoopy = new Snoopy;
// need an proxy?://
$snoopy->proxy_host = "my.proxy.host";
$snoopy->proxy_port = "8080";
// set browser and referer:
$snoopy->agent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)";
$snoopy->referer = "http://www.jonasjohn.de/";
// set some cookies:
$snoopy->cookies["SessionID"] = '238472834723489';
$snoopy->cookies["favoriteColor"] = "blue";
// set an raw-header:
$snoopy->rawheaders["Pragma"] = "no-cache";
// set some internal variables:
$snoopy->maxredirs = 2;
$snoopy->offsiteok = false;
$snoopy->expandlinks = false;
// set username and password (optional)//
$snoopy->user = "joe";
snoopy->pass = "bloe";
// fetch the text of the website www.google.com:
if($snoopy->fetchtext("http://www.google.com"))
{ // other methods: fetch, fetchform, fetchlinks, submittext and submitlinks
// response code: print "response code: ".$snoopy->response_code."<br/>\n";
// print the headers: print "<b>Headers:</b><br/>";
while(list($key,$val) = each($snoopy->headers))
{ 
print $key.": ".$val."<br/>\n";
} 
print "<br/>\n";
// print the texts of the website: print "<pre>".htmlspecialchars($snoopy->results)."</pre>\n";
}
else
{ print "Snoopy: error while fetching document: ".$snoopy->error."\n"; 
}
?>