A verification code error occurred when submitting the login information in curl.

Source: Internet
Author: User
Verification Code. Currently, you can find the URL and directly display it on the front-end webpage. Then, enter it manually. You can use AJAX to submit the login information on the webpage and return the correct information. I transfer the same combination of information to the PHP file in the background and submit it with curl. However, the verification code error is reported every time. It seems that the verification code is in php. Currently, the URL is found and displayed directly on the front-end webpage, and then manually entered.
You can use AJAX to submit the login information on the webpage and return the correct information.
I transfer the same combination of information to the PHP file in the background and submit it with curl. However, the verification code error is reported every time.
It seems that I submitted and refreshed the verification code in php. How can I solve this problem?

I tried using the code of ty0716, but there are still some problems. I don't know how to solve them.
Directly obtain the Verification Code address. No cookie is saved. The address on the login page cannot be identified by the Verification code.

Define ('script _ root', dirname (_ FILE __). '/'); include "code. php "; function send ($ loginUrl, $ cookieFile) {// $ name = $ _ POST ['name']; // $ idcard =$ _ POST ['idcard']; // $ pw =$ _ POST ['PW ']; // $ code = $ _ POST ['code']; $ code = getCode (' http://www.xxx.com/Num.jsp '); Header ("Content-Type: text/html; charset = UTF-8"); $ post_data = "". $ code; return post ($ loginUrl, $ post_data, $ cookieFile);} function getCode ($ captchaUrl, $ cookieFile) {$ captchaString = get ($ captchaUrl, $ cookieFile ); $ tempCaptchaFile = SCRIPT_ROOT. date ('ymdhis '). '.jpg '; file_put_contents ($ tempCaptchaFile, $ captchaString); $ ocr = new code ($ tempCaptchaFile); $ captcha = $ ocr-> getCode (); echo $ captcha; return $ captcha;} function get ($ url, $ cookie_file, $ isCookiesSave = false) {// initialize $ curl = curl_init ($ url); $ header = array (); $ header [] = 'user-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/80'; curl_setopt ($ curl, CURLOPT_HTTPHEADER, $ header); // do not output header information curl_setopt ($ curl, CURLOPT_HEADER, 0); if ($ isCookiesSave) {curl_setopt ($ curl, CURLOPT_COOKIEJAR, $ cookie_file);} else {curl_setopt ($ curl, CURLOPT_COOKIEFILE, $ cookie_file);} // save it to a string instead of outputting curl_setopt ($ curl, CURLOPT_RETURNTRANSFER, 1 ); // whether to capture the redirected page curl_setopt ($ curl, CURLOPT_FOLLOWLOCATION, 1); $ info = curl_exec ($ curl); curl_close ($ curl); return $ info ;} function post ($ url, $ data, $ cookie_file) {// initialize $ curl = curl_init ($ url); $ header = array (); $ header [0] = 'user-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/100 '; curl_setopt ($ curl, CURLOPT_HTTPHEADER, $ header); // do not output header information curl_setopt ($ curl, CURLOPT_HEADER, 0); // save it to a string instead of curl_setopt ($ curl, cursor, 1); curl_setopt ($ curl, CURLOPT_COOKIEFILE, $ cookie_file); // post Data curl_setopt ($ curl, CURLOPT_POST, 1); // request data curl_setopt ($ curl, CURLOPT_POSTFIELDS, $ data); // whether to capture the redirected page curl_setopt ($ curl, CURLOPT_FOLLOWLOCATION, 1); $ response = curl_exec ($ curl); curl_close ($ curl ); return $ response ;}

Reply content:

Verification Code. Currently, you can find the URL and directly display it on the front-end webpage. Then, enter it manually.
You can use AJAX to submit the login information on the webpage and return the correct information.
I transfer the same combination of information to the PHP file in the background and submit it with curl. However, the verification code error is reported every time.
It seems that I submitted and refreshed the verification code in php. How can I solve this problem?

I tried using the code of ty0716, but there are still some problems. I don't know how to solve them.
Directly obtain the Verification Code address. No cookie is saved. The address on the login page cannot be identified by the Verification code.

Define ('script _ root', dirname (_ FILE __). '/'); include "code. php "; function send ($ loginUrl, $ cookieFile) {// $ name = $ _ POST ['name']; // $ idcard =$ _ POST ['idcard']; // $ pw =$ _ POST ['PW ']; // $ code = $ _ POST ['code']; $ code = getCode (' http://www.xxx.com/Num.jsp '); Header ("Content-Type: text/html; charset = UTF-8"); $ post_data = "". $ code; return post ($ loginUrl, $ post_data, $ cookieFile);} function getCode ($ captchaUrl, $ cookieFile) {$ captchaString = get ($ captchaUrl, $ cookieFile ); $ tempCaptchaFile = SCRIPT_ROOT. date ('ymdhis '). '.jpg '; file_put_contents ($ tempCaptchaFile, $ captchaString); $ ocr = new code ($ tempCaptchaFile); $ captcha = $ ocr-> getCode (); echo $ captcha; return $ captcha;} function get ($ url, $ cookie_file, $ isCookiesSave = false) {// initialize $ curl = curl_init ($ url); $ header = array (); $ header [] = 'user-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/80'; curl_setopt ($ curl, CURLOPT_HTTPHEADER, $ header); // do not output header information curl_setopt ($ curl, CURLOPT_HEADER, 0); if ($ isCookiesSave) {curl_setopt ($ curl, CURLOPT_COOKIEJAR, $ cookie_file);} else {curl_setopt ($ curl, CURLOPT_COOKIEFILE, $ cookie_file);} // save it to a string instead of outputting curl_setopt ($ curl, CURLOPT_RETURNTRANSFER, 1 ); // whether to capture the redirected page curl_setopt ($ curl, CURLOPT_FOLLOWLOCATION, 1); $ info = curl_exec ($ curl); curl_close ($ curl); return $ info ;} function post ($ url, $ data, $ cookie_file) {// initialize $ curl = curl_init ($ url); $ header = array (); $ header [0] = 'user-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/100 '; curl_setopt ($ curl, CURLOPT_HTTPHEADER, $ header); // do not output header information curl_setopt ($ curl, CURLOPT_HEADER, 0); // save it to a string instead of curl_setopt ($ curl, cursor, 1); curl_setopt ($ curl, CURLOPT_COOKIEFILE, $ cookie_file); // post Data curl_setopt ($ curl, CURLOPT_POST, 1); // request data curl_setopt ($ curl, CURLOPT_POSTFIELDS, $ data); // whether to capture the redirected page curl_setopt ($ curl, CURLOPT_FOLLOWLOCATION, 1); $ response = curl_exec ($ curl); curl_close ($ curl ); return $ response ;}

When you obtain the page for the first time, you need to save the cookie, and then post your login information and verification code together with the cookie. If you directly post the verification code, your verification code always corresponds to the last verification code.

After reading, such a verification code can be a verification code identification, refer to my article: https://segmentfault.com/a/1190000004361370

Make sure that the session_id of the submitted post is consistent with the session_id of the Verification Code. Otherwise, the verification code is incorrect.

The verification code is output by your program,

After obtaining the verification code using php, you can call the verification code again. It is either an expired verification code or your session_id verification code.

Find the URL of the Verification Code, request a verification code when submitting the request, listen to the recognition, or manually enter

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.