Php uses curl to simulate logon to a website with a verification code. curl verification code _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
Php uses curl to simulate logon to a website with a verification code, curl verification code. Php uses curl to simulate logon to a website with a verification code. the curl verification code is required to log on to a website with the verification code to obtain data. However, it is impossible to record data manually, I want php to use curl to simulate login to a website with a verification code, curl verification code

This is the requirement. you need to log on to the website with the verification code to obtain the data, but it is impossible to record the data manually. The following is the result code of the test! For more information, see!

 Display ();} public function _ initialize () {foreach ($ this-> cookieName as $ key => $ name) {$ this-> cookiePathFile [] = ROOT_PATH. $ this-> cookiePath. $ this-> cookieName [$ key]. '_xxx.txt ';}/*** log on to xxx */public function xxxLogin () {$ username = I ('Username'); $ password = I ('password '); $ verifyCode = I ('verify '); $ loginData = array (' _ viewstate' => '/logs/yae69nsy162133yup0lxjz8oxu ', // if the parameter is not supplemented, it may not be responded. '_ viewstategenerator' => 'dc42de27', 'txt _ username' => $ UserName, 'txt _ pwd' => $ password, 'txt _ verifycode' => $ VerifyCode, 'smoney '=> 'ABC', 'login _ Submit. x' => '52 ', 'login _ Submit. y' => '19',); $ getBack = $ this-> _ cookieRequest (' http://xxx.com/noLogin.aspx ', $ LoginData); if (preg_match ('/
 
  
(.*?) <\/P>/S', $ getBack, $ match) {echo 'matched \ r \ n'; print_r ($ match);} else {echo $ getBack ,'
  
'; $ ParamsFull = parse_url ($ getBack); parse_str ($ paramsFull ['query'], $ paramsFull ['parsedquery']); if (! Empty ($ paramsFull ['parsedquery'] ['warn']) {$ msg = "Hello, welcome to P. please log on first. "; Switch ($ paramsFull ['parsedquery'] ['warn']) {case '2': $ msg = 'Your verification code is incorrect. please try again '; break; case '3': $ msg = 'this account does not exist. do you have another account? '; Break; case '5': $ msg = 'account canceled'; break; case '6': $ msg = 'incorrect password, if an error occurs three times in a row, you cannot log on within 30 minutes! '; Break; case '20': $ msg =' today, the password has been wrong three times or more. please log on again in half an hour! '; Break; case '21': $ msg =' all accounts and passwords of your IP address are incorrect more than 9 times today. please log on again in half an hour! '; Break; case '22': $ msg =' logon failed. your IP address has too many logon accounts today! '; Break; case '23': $ msg =' logon failed. the verification code is invalid! '; Break; case '32': $ msg =' this account has been bound to another xx account! '; Break; case '33': $ msg =' one computer can only register one account per day! '; Break;} $ this-> error ($ msg, '', 5);} else {$ _ SESSION ['User _ id'] = '2016 '; // Set the logon session $ this-> success ('logon to the P website successfully', U ('index/index'), 5 );}}} /*** get the verification code */public function getVerifyCode () {$ img = $ this-> _ cookieRequest (' http://xxx.com/VerifyCode_Login.aspx?id= '. Rand (Random, 999999), null, true, 1); echo $ img;}/*** delete cookie */public function clearCookie () {for ($ I = 0; $ I CookieName); $ I ++) {setcookie ($ this-> cookieName [$ I], '', time ()-3600 );} // unlink ($ this-> cookiePathFile); $ this-> success ('cookie cleared successfully! ');} /*** Access curl with COOKIE * @ param $ url access address * @ param bool | array $ data transmitted data * @ param bool $ redirect whether to obtain the redirected address *@ return mixed address or returned content */public function _ cookieRequest ($ url, $ data = null, $ redirect = false, $ cookieNum = 0) {$ ch = curl_init (); $ params [CURLOPT_URL] = $ url; // request url $ params [CURLOPT_HEADER] = false; // whether to return the response header information $ params [CURLOPT_RETURNTRANSFER] = true; // whether to return the result $ params [CURLOPT_FOL LOWLOCATION] = true; // whether to redirect $ params [CURLOPT_USERAGENT] = 'mozilla/5.0 (Windows NT 5.1; rv: 9.0.1) Gecko/20100101 Firefox/9.0.1 '; if ($ data) {$ params [CURLOPT_POST] = true; $ params [CURLOPT_POSTFIELDS] = http_build_query ($ data);} // checks whether a cookie exists, if yes, use if (! Empty ($ _ COOKIE [$ this-> cookieName [$ cookieNum]) & is_file ($ this-> cookiePathFile [$ cookieNum]) {$ params [CURLOPT_COOKIEFILE] = $ this-> cookiePathFile [$ cookieNum]; // Determine the cookie here} else {// $ cookie_jar = tempnam ($ cookie_path, 'cooke '); // Generate a cookie file $ params [CURLOPT_COOKIEJAR] = $ this-> cookiePathFile [$ cookieNum]; // write cookie information setcookie ($ this-> cookieName [$ cookieNum], $ this-> cookiePathFile [$ cookieNum], t Ime () + 120); // save cookie path} curl_setopt_array ($ ch, $ params); // input the curl parameter $ content = curl_exec ($ ch ); $ headers = curl_getinfo ($ ch); // echo $ content; curl_close ($ ch); if ($ url! = $ Headers ["url"] & $ redirect = false) {return $ headers ["url"] ;}return $ content ;}}

After logging on, you can access other pages with cookies!

  

You need to log on to the website with the verification code to obtain data, but it is impossible to record data manually...

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.