使用php snoopy 类GET/POST请求模拟登陆

2018-05-14网页设计
179

snoopy是一个php类,用来模仿web浏览器的功能,它能完成获取网页内容和发送表单的任务。
 
官方网站 http://snoopy.sourceforge.net/
 
下面是它的一些特征:
 
方便抓取网页的内容
方便抓取网页的文字(去掉HTML代码)
方便抓取网页的链接
支持代理主机
支持基本的用户/密码认证模式
支持自定义用户agent,referer,cookies和header内容
支持浏览器转向,并能控制转向深度
能把网页中的链接扩展成高质量的url(默认)
方便提交数据并且获取返回值
支持跟踪HTML框架(v0.92增加)
支持再转向的时候传递cookies
下面是简单的例子,比如说我们抓取我的blog的文字
 
 
<?php
include "Snoopy.class.php"; 
$snoopy = new Snoopy; 
$snoopy->fetchtext("http://www.xxxx.com/blog"); 
echo $snoopy->results; 
?>
 
^_^,不错把,在比如抓取链接
 
 
<?php
include "Snoopy.class.php"; 
$snoopy = new Snoopy; 
$snoopy->fetchlinks("http://www.xxxx.com/blog"); 
print_r($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.xxxx.com/";
// 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";    
}
?>
首先,我们要获取到登陆需要发送什么字段,目标地址是什么。这里我们使用snoopy的fetchform来实现。
 
 
<? 
include "Snoopy.class.php"; 
$snoopy = new Snoopy; 
$snoopy->fetchform("http://www.xxxx.com/login.php?action=login"); 
print $snoopy->results; 
?>
当然你也可以直接查看http://www.phpx.com/happy/...的源代码来实现,不过这样更加方便把。这里,我们获取到目标和提交的数据,下一步就可以实现模拟登陆了。代码如下:
 
<? 
include "Snoopy.class.php"; 
$snoopy = new Snoopy; 
$submit_url = "http://www.xxxx.com/login.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;
?>

The End

相关推荐

移动端,手机端 响应式布局头部代码
将如下代码加到你网页的头部之间: meta name=viewport content=width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no meta name=apple-mobile-web-app-capable content=yes / meta name=apple-mobile-web-app-statu...
2021-06-10 网页设计
262

js 判断字符串中是否包含某个字符串
String对象的方法 方法一: indexOf() (推荐) var str = 123;console.log(str.indexOf(3) != -1 ); // trueindexOf() 方法可返回某个指定的字符串值在字符串中首次出现的位置。如果要检索的字符串值没有出现,则该方法返回 -1。 方法二: search() var str = 1...
2019-05-30 网页设计
262

让网页标签页头部有新消息提醒和Title标题文字滚动显示效果
demo展示1: !DOCTYPE html PUBLIC -//W3C//DTD XHTML 1.0 Transitional//EN http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtdhtml xmlns=http://www.w3.org/1999/xhtml head meta http-equiv=Content-Type content=text/html; charset=utf-8 / tit...
2019-05-18 网页设计
149

怎么拿到input file上传的文件名字显示到页面中
js代码部分: $(.aFileBtn).on(change,input[type=file],function(){ var filePath=$(this).val(); //filePath.indexOf(jpg)!=-1 || filePath.indexOf(png)!=-1 if(filePath.length 0){ $(.fileerrorTip).html().hide(); var arr=filePath.split(\\); var fi...
2019-05-15 网页设计
164

swiper无法自动播放的解决办法
1、首先看下自己网页中的js是否有异常,找不到js情况。 2、如果确认无异常的话,可能是你没有调用swiper的自动播放代码,请将以下代码加入到您网页中 script type=text/javascript $(function(){ var swiper = new Swiper(.swiper-container, { pagination:...
2019-04-10 网页设计
507

bootstrap在移动浏览器下响应式不起作用的解决方法
Bootstrap是Twitter推出的一个开源的用于前端开发的工具包。它由Twitter的设计师Mark Otto和Jacob Thornton合作开发,是一个CSS/HTML框架。Bootstrap提供了优雅的HTML和CSS规范,它即是由动态CSS语言Less写成。Bootstrap一经推出后颇受欢迎,一直是GitHub上...
2019-03-07 网页设计
128