PHP uses curl to implement a mock login to a website with a CAPTCHA

Source: Internet
Author: User

Many of the online simulation login programs, mostly through the service program Apache and other operations, get to the verification code displayed on the Web page, and then fill in and then post out, although it looks very friendly, but since the simulation login, after the log on the thing is not necessarily a short time to complete, So this is subject to PHP's maximum execution time limit, and some operations may not have enough permissions.

This article provides a program example, the idea is to obtain a verification code after the verification code stored as a picture, and then the program sleeps 20 seconds, after 20 seconds by the user manually view the picture, and to fill out the verification code To the Code.txt file, after 20 seconds of hibernation is completed, the program will read the Code.txt verification code, so that with the verification code for the login operation. The specific code is as follows:

?
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 /** * 模拟登录 *///初始化变量$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,开始做你想做的事吧。。。。。

PHP uses curl to implement a mock login to a website with a CAPTCHA

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.