CURL can be described as a must-have killer medicine for home travel. Why is it so described? It is because it is easy to use and can realize a series of functions such as page grabbing, simulated login collection and so on.
I remember that the first time I came into contact with CURL was to complete the crawling from the mailbox user list. At that time, in order to catch up with the progress, I didn't study it in detail. I just found some information on the Internet and implemented the function. Now after organizing the original code, the function can still be used
error_reporting ( 0 );
set_time_limit ( 0 );
header ( "Content-Type: text/html; charset=GB2312" );
//Email username and password
$user = 'username';
$pass = 'password';
//Create a file to store cookie information
define ( "COOKIEJAR", tempnam ( ini_get ( "upload_tmp_dir" ), " cookie" ) );
$url = 'http://reg.163.com/logins.jsp?type=1&url=http://entry.mail.163.com/coremail/fcg/ntesdoor2 ?lightweight%3D1%26verifycookie%3D1%26language%3D-1%26style%3D-1';
$refer = 'http://mail.163.com';
$fields_post = array ('username ' => $user, 'password' => $pass, 'verifycookie' => 1, 'style' => - 1, 'product' => 'mail163', 'selType' => - 1, 'secure' => 'on' );
$fields_string = http_build_query ( $fields_post, '&' );
$headers_login = array ('User-Agent' => 'Mozilla/5.0 ( Windows; U; Windows NT 5.1; zh-CN; rv:1.9) Gecko/2008052906 Firefox/3.0', 'Referer' => 'http://www.163.com' );
/ /Login
$ch = curl_init ( $url );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt ( $ch, CURLOPT_HEADER, true );
curl_setopt ( $ch, CURLOPT_CONNECTTIMEOUT, 120 );
curl_setopt ( $ch, CURLOPT_POST, true );
curl_setopt ( $ch, CURLOPT_REFERER, $refer );
curl_setopt ( $ch, CURLOPT_COOKIESESSION, true );
curl_setopt ( $ch , CURLOPT_COOKIEJAR, COOKIEJAR );
curl_setopt ( $ch, CURLOPT_HTTPHEADER, $headers_login );
curl_setopt ( $ch, CURLOPT_POST, count ( $fields ) );
curl_setopt ( $ch, CURLOPT_POSTFIELDS, $fields _string ) ;
$result = curl_exec ( $ch );
curl_close ( $ch );
//Jump
$url = 'http://entry.mail.163.com /coremail/fcg/ntesdoor2?lightweight=1&verifycookie=1&language=-1&style=-1&username=loki_wuxi';
$headers = array ('User-Agent' => 'Mozilla/5.0 (Windows; U; Windows NT 5.1 ; zh-CN; rv:1.9) Gecko/2008052906 Firefox/3.0' );
$ch = curl_init ( $url );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt ( $ch, CURLOPT_HEADER, true );
curl_setopt ( $ch, CURLOPT_CONNECTTIMEOUT, 120 );
curl_setopt ( $ch, CURLOPT_POST, true );
curl_setopt ( $ch, CURLOPT_HTTPHEADER, $headers );
curl_setopt ( $ch, CURLOPT_COOKIEFILE, COOKIEJAR );
curl_setopt ( $ch, CURLOPT_COOKIEJAR, COOKIEJAR );
$result = curl_exec ( $ch );
curl_close ( $ch );
//Get sid
preg_match ( '/sid=[^"].*/', $result, $location );
$sid = substr ( $location [0], 4, - 1 ) ;
//Address book address
$url = 'http://g4a30.mail.163.com/jy3/address/addrlist.jsp?sid=' . $sid . '&gid=all ';
$headers = array ('User-Agent' => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9) Gecko/2008052906 Firefox/3.0' );
$ch = curl_init ( $url );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt ( $ch, CURLOPT_HEADER, true );
curl_setopt ( $ch, CURLOPT_CONNECTTIMEOUT, 120 );
curl_setopt ( $ch, CURLOPT_POST, true );
curl_setopt ( $ch, CURLOPT_HTTPHEADER, $headers );
curl_setopt ( $ch, CURLOPT_COOKIEFILE, COOKIEJAR );
curl_setopt ( $ch, CURLOPT_COOKIEJAR, COOKIEJAR );
$result = curl_exec ( $ch );
curl_close ( $ch );
unlink ( COOKIEJAR );
//Start crawling content
preg_match_all ( '/
]*>(.*?) | < ;a[^>]*>(.*?) | /i', $result, $infos, PREG_SET_ORDER );
//1: Name 2: Email
print_r ( $infos );
?>
Create a PHP file, copy the above code and save it. The effect will be immediate. Remember to change your email account and password. The account does not need the @ suffix. My first experience with CURL, how about it, not bad.
Later, I saw someone posting on CSDN asking a question about getting express delivery queries. He wanted to put some large express delivery company query services on one page. It is indeed a very good and practical tool, but because express delivery queries have The verification code reminds me of the CURL tool. Later, I helped the post owner implement the function. The idea was very simple. First, use CURL to simulate grabbing the verification code, and then display it on the user submission page. At the same time, the COOKIE and other user queries that save the verification code are submitted together to ensure the synchronization of COOKIE.