php模拟登录二个例子(snoopy模拟登录与curl模拟登录)

发布时间:2020-08-02编辑:脚本学堂
有关php模拟登录的二个例子,分别使用snoopy与curl来实现模拟登录,例子很简单,用来学习研究php模拟登录的实现方法,有一定参考价值。

php模拟登录的实现方法,php模拟登录人人网。

1、snoopy模拟登录:
 

复制代码 代码示例:
<?php
set_time_limit(0);
require "Snoopy.class.php";
$snoopy=new Snoopy();
$snoopy->referer='http://www.plcxue.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.plcxue.com/test/Login.php';//登录数据提交的url地址
$snoopy->submit($url,$submit_vars);
$snoopy->fetch("http://www.plcxue.com/");//希望获取的页面数据
echo $snoopy->results;//m.plcxue.com

2、php curl模拟登录:
 

复制代码 代码示例:
<?php
set_time_limit(0);
$cookie_file=tempnam('./tmp','cookie');//tmp目录需要先建立好
$ch=curl_init();
$login_url='http://www.plcxue.com/PLogin.do';
$curlPost="email=登录账号&password=登录密码";
curl_setopt($ch,CURLOPT_URL,$login_url);
//启用时会将头文件的信息作为数据流输出
curl_setopt($ch,CURLOPT_HEADER,0); //设定是否输出页面内容
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_POST,1); //设置请求发送方式,post或get,CURLOPT_POST或CURLOPT_GET
curl_setopt($ch,CURLOPT_POSTFIELDS,$curlPost);
curl_setopt($ch,CURLOPT_COOKIEJAR,$cookie_file); //保存cookie
curl_exec($ch);
curl_close($ch);
$ch=curl_init();
$login_url2="http://www.plcxue.com/";
curl_setopt($ch,CURLOPT_URL,$login_url2);
curl_setopt($ch,CURLOPT_HEADER,0);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,0);
curl_setopt($ch,CURLOPT_COOKIEFILE,$cookie_file); //读取cookie
curl_exec($ch);
curl_close($ch);