curl模拟请求、登陆以及带验证码登陆

时间:2016-11-16 22:21:45

header('content-type:text/html;charset=utf-8');    

function curlPost($url,$data,$method){    

    $ch = curl_init();   //1.初始化    

    curl_setopt($ch, CURLOPT_URL, $url); //2.请求地址    

    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);//3.请求方式    

    //4.参数如下    

    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);//https    

    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);    

    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');//模拟浏览器    

    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);    

    curl_setopt($ch, CURLOPT_AUTOREFERER, 1);    

        curl_setopt($ch, CURLOPT_HTTPHEADER,array('Accept-Encoding: gzip, deflate'));//gzip解压内容    

        curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');    

         

    if($method=="POST"){//5.post方式的时候添加数据    

        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);    

    }    

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);    

    $tmpInfo = curl_exec($ch);//6.执行    

     

    if (curl_errno($ch)) {//7.如果出错    

        return curl_error($ch);    

    }    

    curl_close($ch);//8.关闭    

    return $tmpInfo;    

}    

$data=array('name' => '1234');    

$url="搜狐";    

     

$method="GET";  //post或者get  

$file=curlPost($url,$data,$method);    

$file=mb_convert_encoding($file,'UTF-8','GBK');    

echo $file;

以下是模拟登陆的代码

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

 $cookie_file = tempnam('./temp','cookie');    

    function weixinPost($url,$data,$method,$setcooke=false,$cookie_file=false){    

        $ch = curl_init();   //1.初始化    

        curl_setopt($ch, CURLOPT_URL, $url); //2.请求地址    

        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);//3.请求方式    

        //4.参数如下        

        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);    

        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);    

        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');    

        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);    

        curl_setopt($ch, CURLOPT_AUTOREFERER, 1);    

             

        if($method=="POST"){//5.post方式的时候添加数据       

            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);    

        }    

        if($setcooke==true){    

            curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);    

        }else{    

            curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);    

        }    

        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);    

        $tmpInfo = curl_exec($ch);//6.执行    

     

        if (curl_errno($ch)) {//7.如果出错    

            return curl_error($ch);    

        }    

        curl_close($ch);//8.关闭    

        return $tmpInfo;    

    }    

    $data=array('username' => '***','password'=>'***');    

    $url="http://www.xinxinj.com/login.php";    

    $method="POST";    

    $file=weixinPost($url,$data,$method,true,$cookie_file);    

    echo $file;    

             

    $url="http://www.xinxinj.com/admin.php";    

    $method="GET";    

    $file=weixinPost($url,$data,$method,false,$cookie_file);    

    echo $file;

网上的很多模拟登录程序,大都是通过服务程序apache之类的运行,获取到验证码之后显示在网页上,然后填上再POST出去

本文提供了一个程序实例,思路就是获取到验证码之后把验证码存储为一个图片,然后程序休眠20秒,在20秒之后由用户手动查看图片,并把验证码填写到code.txt文件中,20秒休眠完成后,程序会读code.txt的验证码,这样再带着验证码进行登录操作。具体代码如下:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

/** 

 * 模拟登录 

 */  

    

//初始化变量  

$cookie_file "tmp.cookie";  

$login_url "http://xxx.com/logon.php";  

$verify_code_url "http://xxx.com/verifyCode.php";  

    

echo "正在获取COOKIE...\n";  

$curlj = curl_init();  

$timeout = 5;  

curl_setopt($curl, CURLOPT_URL, $login_url);  

curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);  

curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, $timeout);  

curl_setopt($curl,CURLOPT_COOKIEJAR,$cookie_file); //获取COOKIE并存储  

$contents = curl_exec($curl);  

curl_close($curl);  

    

echo "COOKIE获取完成,正在取验证码...\n";  

//取出验证码  

$curl = curl_init();  

curl_setopt($curl, CURLOPT_URL, $verify_code_url);  

curl_setopt($curl, CURLOPT_COOKIEFILE, $cookie_file);  

curl_setopt($curl, CURLOPT_HEADER, 0);  

curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);  

$img = curl_exec($curl);  

curl_close($curl);  

    

$fp fopen("verifyCode.jpg","w");  

fwrite($fp,$img);  

fclose($fp);  

echo "验证码取出完成,正在休眠,20秒内请把验证码填入code.txt并保存\n";  

//停止运行20秒  

sleep(20);  

    

echo "休眠完成,开始取验证码...\n";  

$code file_get_contents("code.txt");  

echo "验证码成功取出:$code\n";  

echo "正在准备模拟登录...\n";  

    

$post "username=maben&pwd=hahahaha&verifycode=$code";  

$curl = curl_init();  

curl_setopt($curl, CURLOPT_URL, $url);  

curl_setopt($curl, CURLOPT_HEADER, false);  

curl_setopt($curl, CURLOPT_RETURNTRANSFER,1);  

curl_setopt($curl, CURLOPT_POSTFIELDS, $post);  

curl_setopt($curl, CURLOPT_COOKIEFILE, $cookie_file);  

$result=curl_exec($curl);  

curl_close($curl);  

    

//这一块根据自己抓包获取到的网站上的数据来做判断  

if(substr_count($result,"登录成功")){  

 echo "登录成功\n";  

}else{  

 echo "登录失败\n";  

 exit;  

}  

    

//OK,开始做你想做的事吧。。。。。

  • 17
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值