snoopy是一个php类,用来模仿web浏览器的功能,它能完成获取网页内容和发送表单的任务。
php使用Snoopy.class.php做http get post请求
<?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 //获取表单 ?>
<?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"] = "提 交"; $snoopy->submit($submit_url,$submit_vars); print $snoopy->results; ?>
<?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 class="brush:php;toolbar:false">".htmlspecialchars($snoopy->results)."\n"; } else { print "Snoopy: error while fetching document: ".$snoopy->error."\n"; } ?>