Home >Backend Development >PHP Tutorial >curl simulated login implementation tutorial_PHP tutorial
Simulated login to 126 mailbox, Renren, etc. Modify it as needed, the code is as follows
//Simulate login to the website with verification code, first obtain the cookie of the verification code image
define('SCRIPT_ROOT',dirname(__FILE__).'/');
$cookieFile = SCRIPT_ROOT.'cookie.tmp';
/*Simulate browser*/
$user_agent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.1.4322)";
//If you know the cookie here, you can send it directly to the target browser
//$cookie = "lang=zh-cn; JSESSIONID=6AF7DA3F6A5FC3ECA39A7485C3FDBBAE";
$header = array (
"Host:login.bioman.com",
"Referer: http://www.bioman.com",
);
//Verification code image url
Function getCookie($url){
global $cookieFile;
$curl = curl_init (); // Start a CURL session
curl_setopt ( $curl, CURLOPT_URL, $url ); // The address to be accessed
curl_setopt ( $curl, CURLOPT_SSL_VERIFYPEER, 0 ); // Check the source of the authentication certificate
curl_setopt ( $curl, CURLOPT_SSL_VERIFYHOST, 1 ); // Check whether the SSL encryption algorithm exists from the certificate
curl_setopt ( $curl, CURLOPT_HTTPGET, 1 ); // Send a regular GET request
curl_setopt($curl,CURLOPT_COOKIEJAR, $cookieFile); // Save the returned cookie information in the file
$res = curl_exec($curl);
curl_close($curl);
}
Function login($url){
global $cookieFile,$header,$agent;
$data = array(
'name'=>'bioman'
);
$curl = curl_init (); // Start a CURL session
curl_setopt ( $curl, CURLOPT_URL, $url ); // The address to be accessed
curl_setopt ( $curl, CURLOPT_SSL_VERIFYPEER, 0 ); // Check the source of the authentication certificate
curl_setopt ( $curl, CURLOPT_SSL_VERIFYHOST, 1 ); // Check whether the SSL encryption algorithm exists from the certificate
curl_setopt ( $curl, CURLOPT_USERAGENT, $agent ); // Simulate the browser used by the user
curl_setopt($curl, CURLOPT_HTTPHEADER, $header); //Where to set header information
@curl_setopt ( $curl, CURLOPT_FOLLOWLOCATION, 1 ); // Use automatic jump
//curl_setopt ( $curl, CURLOPT_HTTPGET, 1 ); // Send a regular GET request
curl_setopt ( $curl, CURLOPT_POST, 1 ); // Send a regular Post request
curl_setopt ( $curl, CURLOPT_POSTFIELDS, $data ); // Post submitted data package
//curl_setopt ( $curl, CURLOPT_COOKIE, $cookie); // Send cookie content directly
curl_setopt($curl,CURLOPT_COOKIEFILE, $cookieFile); //Send Cookie file
curl_setopt ( $curl, CURLOPT_TIMEOUT, 120 ); // Set timeout limit to prevent infinite loop
curl_setopt ( $curl, CURLOPT_HEADER, 0 ); // Do not display the returned Header area content
curl_setopt ( $curl, CURLOPT_RETURNTRANSFER, 1 ); // The obtained information is returned in the form of a file stream
$res = curl_exec ($curl); // Execute the operation
if (curl_errno ( $curl )) {
Return 'Failure: Errno' . curl_error ( $curl );
}
curl_close ( $curl ); // Close CURL session
return $res;
}
//1.
$auth_url = "http://www.bioman.com/auth";
getCookie($auth_url); //Get verification code cookie
//2.
$url = 'http://www.bioman.com/home'; //url submitted after logging in
$res = login($url);
print_r($res);