PHP uses CURL to simulate logon to a website with a verification code.
Many simulated login programs on the Internet mostly run through service programs such as apache. After obtaining the verification code, they are displayed on the webpage and then filled in and POST the code. Although this looks friendly, however, since simulated logon does not necessarily take a short time to complete the tasks after logon, this is subject to the maximum execution time of php, and some operations may have insufficient permissions.
Token verification code, and then carry the verification code to log on. The Code is as follows:
/*** Simulate logon * // initialization variable $ cookie_file = "tmp. cookie "; $ login_url =" http://xxx.com/logon.php "; $ verify_code_url =" http://xxx.com/verifyCode.php "; echo" getting 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); // obtain the COOKIE and Store $ contents = curl_exec ($ curl); curl_close ($ curl); echo "the COOKIE is obtained, and the verification code is being obtained... \ n "; // obtain the verification code $ 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); ec Ho "Verification Code retrieval is complete, just waiting for sleep, please fill the verification code into code.txt within 20 seconds and save \ n"; // stop running for 20 seconds sleep (20); echo "sleep complete, start to get verification code... \ n "; $ code = file_get_contents (" code.txt "); echo" Verification code retrieved successfully: $ code \ n "; echo" Preparing to simulate logon... \ 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_POSTF IELDS, $ post); curl_setopt ($ curl, CURLOPT_COOKIEFILE, $ cookie_file); $ result = curl_exec ($ curl); curl_close ($ curl ); // This part determines if (substr_count ($ result, "Logon successful") {echo "Logon successful \ n ";} else {echo "Logon Failed \ n"; exit;} // OK, start what you want to do .....
Php curl post simulates the user login and hopes to provide specific code and briefly explain the code
You can check whether other http headers are not simulated, such as whether Referer and User-Agent can simulate the browser value. A complete request is similar to this:
GET/home/pack/data/content? Id = 0.03439752989200834, 1.1, & asyn = 1 & t = & _ req_seqid = 0xa982225f0637c78a HTTP/
Accept :*/*
Accept-Language: zh-cn
Referer: www.baidu.com/
X-requested-with: XMLHttpRequest
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; BTRS123401; InfoPath.2 ;. net clr 2.0.50727 ;. net clr 3.0.20.6.2152 ;. net clr 3.5.30729 ;. NET4.0C; MS-RTC LM 8)
Host: www.baidu.com
Connection: Keep-Alive
Cookie: XCXXXXX
How does curl simulate php login?
$ Url = "localhost/post_output.php ";
$ Post_data = array (
"Foo" => "bar ",
"Query" => "Nettuts ",
"Action" => "Submit"
);
$ Ch = curl_init ();
Curl_setopt ($ ch, CURLOPT_URL, $ url );
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1 );
// We are posting data!
Curl_setopt ($ ch, CURLOPT_POST, 1 );
// Add the post variable
Curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ post_data );
$ Output = curl_exec ($ ch );
Curl_close ($ ch );
Echo $ output;
Result