Detailed description of obtaining email addresses based on PHPCURL

Source: Internet
Author: User
Tags response code set cookie email account
This article provides a detailed analysis of PHP's use of CURL to obtain the email address. if you need a friend, refer to CURL, which is essential for Home Travel. why is it so described? It is because of its ease of use that it can easily capture pages, simulate logon collection, and other functions.
Remember that the first time you contact CURL, you must capture the list of Mailbox users. At that time, in order to catch up with the progress, I did not study it carefully, but I found some information online to implement the function. Now we can sort out the original code and the function is still usable.
The code is as follows:
Error_reporting (0 );
Set_time_limit (0 );
Header ("Content-Type: text/html; charset = GB2312 ");

// Email user name 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% 26 verifycookie % 3D1% 26 language % 3D-1% 26 style % 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 ');

// Log on
$ Ch = curl_init ($ url );
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, true );
Curl_setopt ($ ch, CURLOPT_HEADER, true );
Currochelle 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 );
Currochelle 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 );

// Obtain the sid
Preg_match ('/sid = [^ \ "]. */', $ result, $ location );
$ Sid = substr ($ location [0], 4,-1 );

// Address book
$ 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 );
Currochelle 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 capturing content
Preg_match_all ('/] *> (.*?) <\/A> <\/td>] *> (.*?) <\/A> <\/td>/I ', $ result, $ infos, PREG_SET_ORDER );
// 1: Name 2: email
Print_r ($ infos );
?>

After you create a php file and copy the above code, the results will be immediate. remember to change your email account and password without the @ suffix. CURL's initial experience is not bad.
Later, I saw someone posting on CSDN asking me a question about how to obtain the express delivery query. he wanted to make some large express delivery company query services on a page, it is indeed a very good utility tool, but because the courier query has a verification code, it reminds me of the CURL tool. To help the Post Master implement the function, the idea is very simple. first use CURL to simulate the capture of the verification code, and then display it to the user submission page, at the same time, the COOKIE that saves the verification code and other user queries are submitted together to ensure COOKIE synchronization.

The source code is as follows:
-GetEms.html
The code is as follows:



EMS Express query


Fclose(fopen('cookie.txt ', 'w'); // The cookie.txt file is used to store the obtained cookie.
$ Cookiejar = realpath('cookie.txt ');
$ Fp = fopen ("example_homepage.txt", "w"); // The example_homepage.txt file is used to store the obtained page content.
$ Ch = curl_init ("http://www. EMS .com.cn/servlet/ImageCaptchaServlet ");
Curl_setopt ($ ch, CURLOPT_FILE, $ fp );
Curl_setopt ($ ch, CURLOPT_COOKIESESSION, 1 );
Curl_setopt ($ ch, CURLOPT_COOKIEJAR, $ cookiejar );
Curl_setopt ($ ch, CURLOPT_COOKIEFILE, $ cookiejar );
Curl_setopt ($ ch, CURLOPT_HEADER, 0 );
Curl_exec ($ ch );
Curl_close ($ ch );
Fclose ($ fp );

// Readfile ($ cookiejar); // view the obtained cookie
// Readfile ("example_homepage.jpg"); // view the obtained image
?>





-Getems. php
The code is as follows:
If ($ _ POST ){
// Cookie file using the previous verification code
$ Cookiejar = realpath('cookie.txt ');
// Get the variable name of myEmsbarCode and verification code
$ Ch = curl_init ("http://www. EMS .com.cn ");
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, true );
Curl_setopt ($ ch, CURLOPT_HEADER, 0 );
Curl_setopt ($ ch, CURLOPT_COOKIEFILE, $ cookiejar );
Curl_setopt ($ ch, CURLOPT_COOKIEJAR, $ cookiejar );
$ Result = curl_exec ($ ch );
Curl_close ($ ch );
Preg_match ("/ /IsU ", $ result, $ myEmsbarCode );
Preg_match ("/<\/span>
$ Parm = array ($ codename [1] => $ _ POST ['code'],
MailNum => $ _ POST ['mailnum'],
MyEmsbarCode => $ myEmsbarCode [1],
ReqCode => 'browsebase'
);

$ Ch = curl_init ("http://www. EMS .com.cn/qcgzOutQueryAction.do ");
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1 );
Curl_setopt ($ ch, CURLOPT_HEADER, 0 );
Curl_setopt ($ ch, CURLOPT_COOKIEFILE, $ cookiejar );
Curl_setopt ($ ch, CURLOPT_COOKIEJAR, $ cookiejar );
Curl_setopt ($ ch, CURLOPT_POST, 1 );
Curl_setopt ($ ch, CURLOPT_REFERER, "http://www. EMS .com.cn ");
Curl_setopt ($ ch, CURLOPT_POSTFIELDS, http_build_query ($ parm ));
$ _ Source = curl_exec ($ ch );
Curl_close ($ ch );

// Handle
Var_dump ($ _ source );
Exit;
}
?>

For details about the parameters of the CURL library, I have recorded a lot of parameters on the Internet.
Function list CURL Library contains 17 functions:
Curl_close: close the CURL session
Curl_copy_handle: copies a CURL session handle, and 3 copies all its parameters.
Curl_errno: returns the last error code.
Curl_error: returns a string that describes the last error of the current session.
Curl_exec: execute the current session
Curl_getinfo: get specific information
Curl_init: initialize a CURL session
Curl_multi_add_handle: add a handle to a multi-connection session
Curl_multi_close: Closes a multi-handle CRUL session.
Curl_multi_exec: executes a multi-handle CURL session.
Curl_multi_getcontent: returns the content after a handle is executed. if CURLOPT_RETURNTRANSFER is set
Curl_multi_info_read: obtains information about all current connections.
Curl_multi_init: initializes a multi-handle session.
Curl_multi_remove_handle: deletes a handle from a multi-handle session.
Curl_multi_select: obtain all bound sockets
Curl_setopt: set CURL transfer options
Curl_version: obtain the CURL version.
Common setting options Boolean options
CURLOPT_AUTOREFERER: When the returned information header contains the redirection information, the forward connection is automatically set.
CURLOPT_BINARYTRANSFER: TRUEtoreturntherawoutputwhenCURLOPT_RETURNTRANSFERisused.
CURLOPT_COOKIESESSION: indicates a new cookie session, ignoring the previously set cookie session
CURLOPT_CRLF: converts a Unix system line break to a Dos line break.
CURLOPT_DNS_USE_GLOBAL_CACHE: use global DNS cache
CURLOPT_FAILONERROR: ignore returned errors
CURLOPT_FILETIME: Get the modification date of the request document, which can be obtained using curl_getinfo.
CURLOPT_FOLLOWLOCATION: Immediately following all redirection information returned by the server
CURLOPT_FORBID_REUSE: After the process is processed, the session is forcibly closed and no longer cached for reuse.
CURLOPT_FRESH_CONNECT: forces a new session to be created, rather than reusing cached sessions.
CURLOPT_HEADER: the returned output contains the response header information.
CURLOPT_HTTPGET: Set the HTTP request method to GET
CURLOPT_HTTPPROXYTUNNEL: establish a connection through an HTTP proxy
CURLOPT_NOBODY: the returned output does not contain document information.
CURLOPT_NOPROGRESS: disable process-level transmission. PHP is automatically set to true.
CURLOPT_NOSIGNAL: Ignore all messages sent to PHP
CURLOPT_POST: sets the POST method to submit data. The POST format is application/x-www-form-urlencoded.
CURLOPT_PUTTRUE: sets the PUT mode to upload files, and sets the CURLOPT_INFILE and CURLOPT_INFILESIZE
CURLOPT_RETURNTRANSFER: returns a string, instead of directly outputting it after calling curl_exec ().
CURLOPT_SSL_VERIFYPEER: SSL verification enabled
CURLOPT_UNRESTRICTED_AUTH: always add the user name and password after the link, and set CURLOPT_FOLLOWLOCATION
CURLOPT_UPLOAD: the option to upload an integer
CURLOPT_BUFFERSIZE: cache size
CURLOPT_CONNECTTIMEOUT: Set the connection time. the default value 0 is unlimited.
CURLOPT_DNS_CACHE_TIMEOUT: The time for storing DNS information in the memory. the default value is 2 minutes.
CURLOPT_INFILESIZE: size of the file uploaded to the remote site
CURLOPT_LOW_SPEED_LIMIT: minimum transmission speed limit andabort.
CURLOPT_LOW_SPEED_TIME: transmission time limit
CURLOPT_MAXCONNECTS: maximum number of persistent connections
CURLOPT_MAXREDIRS: maximum number of turns
CURLOPT_PORT: connection port
CURLOPT_PROXYAUTH: ***** verification method
CURLOPT_PROXYPORT: ***** Port
CURLOPT_PROXYTYPE: ***** type
CURLOPT_TIMEOUT: The maximum execution time string option of the CURL function
CURLOPT_COOKIE: cookie information in set-cookie in the HTTP header
CURLOPT_COOKIEFILE: the file containing cookie information. the cookie file format can be Netscape or only the HTTP header format.
CURLOPT_COOKIEJAR: the file that stores the cookie information after the connection ends.
CURLOPT_CUSTOMREQUEST: customizes the request header and uses the relative address.
CURLOPT_ENCODING: the value of Accept-Encoding in the HTTP request header
CURLOPT_POSTFIELDS: data content submitted in POST format
CURLOPT_PROXY: proxy Channel
CURLOPT_PROXYUSERPWD: Proxy Authentication username and password
CURLOPT_RANGE: the range of returned data, in bytes
CURLOPT_REFERER: forward link
CURLOPT_URL: the URL to be connected. you can set it in curl_init ().
CURLOPT_USERAGENT: User-Agent value in the HTTP header
CURLOPT_USERPWD: array of verification information used by connection types
CURLOPT_HTTP200ALIASES: 200 response code array. is the response in the array correct?
CURLOPT_HTTPHEADER: The custom request header information can only be a stream handle option:
CURLOPT_FILE: transfers the handle for writing at night. The default value is standard output.
CURLOPT_INFILE: transfer the file handle to be read
CURLOPT_STDERR: a replacement option for standard error output.
CURLOPT_WRITEHEADER: specifies the file callback function to be written in the transmission header information.
CURLOPT_HEADERFUNCTION: callback function with two parameters. The first parameter is the session handle, and the second is the string of the HTTP response header information. With this callback function, the response header information is automatically processed. Response header information is returned by line. Set the return value to the string length.
CURLOPT_READFUNCTION: callback function with two parameters. The first parameter is the session handle, and the second is the string of the HTTP response header information. Using this function, you can process the returned data. The returned value is the data size.
CURLOPT_WRITEFUNCTION: callback function with two parameters. The first parameter is the session handle, and the second is the string of the HTTP response header information. With this callback function, the response header information is automatically processed. The response header is the entire string. Set the return value to the string length.
Other CURL examples (from the network)
The code is as follows:
/*
* Determine whether a url is a valid link
*/
Function isRealUrl ($ url ){
$ Ch = curl_init ();
$ Options = array (
CURLOPT_URL => $ url,
CURLOPT_HEADER => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_NOBODY => true
);
Curl_setopt_array ($ ch, $ options );
Curl_exec ($ ch );
If (! Curl_errno ($ ch )){
Return 200 = curl_getinfo ($ ch, CURLINFO_HTTP_CODE )? True: false;
}
Curl_close ($ ch );
}

$ Url = 'http: // response ';
If (isRealUrl ($ url) {echo 'yes';} else {echo 'no ';}

/Asynchronous request example:
$ Userid = 517932781;
$ Imageid = 1520;
$ Albumid = 2637;
$ Tags = 'a ';
Extract ($ _ POST );
$ Url = 'http: // '. $ _ SERVER ['http _ host'].'/ajax/image. php ';
$ Fields = array (
'Userid' => $ userid,
'Imageid' => $ imageid,
'Albumid' => $ albumid,
'Tags' => $ tags,
'Optype' => 'Del'
);
$ Ch = curl_init ();
Curl_setopt ($ ch, CURLOPT_URL, $ url );
Curl_setopt ($ ch, CURLOPT_POST, true );
Curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ fields );
$ Result = curl_exec ($ ch );
Curl_close ($ ch );

// Upload a file
$ Ch = curl_init ();
Curl_setopt ($ ch, CURLOPT_URL, 'http: // lh.tom.com/deal/import.php ');
$ Fields = array (
'Tname' => 'Sutra ',
'Country' => 1,
'Author' => 'Lao Tzu ',
'Tags' => 'Sutra ',
'Desc' => 'can be a channel, very Road. It can be a very famous name. The beginning of the unknown world. Famous Mother of everything. Therefore, we often have no desire to look at it. We often want to see it. The two are the same and different, that is, Xuan Zhi. Xuanzhi and xuanjicang. ',
'Volume '=> 2,
'Cover' => '@'. realpath ('/data/lianhuanhua/deal/1.jpg ')
);
Curl_setopt ($ ch, CURLOPT_POST, true );
Curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ fields );
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, false );
$ Result = curl_exec ($ ch );
Curl_close ($ ch );

// Multifile Upload
$ Ch = curl_init ();
Curl_setopt ($ ch, CURLOPT_URL, 'http: // lh.tom.com/deal/addpic.php ');
$ J = 0;
$ Fields = array (
'Vid' = & gt; 103,
'Upfile ['. $ j ++.'] '=>' @ '. realpath ('/data/lianhuanhua/deal/1.jpg '),
'Upfile ['. $ j ++.'] '=>' @ '. realpath ('/data/lianhuanhua/deal/2.jpg ')
);
Curl_setopt ($ ch, CURLOPT_POST, true );
Curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ fields );
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, false );
$ Result = curl_exec ($ ch );
Curl_close ($ ch );

When you have mastered the php curl library, you can do a lot of things you want to do, I directly wrote a combat assistant, which is very easy to use. this code is not open-source. :) The principle is the same as that of open-source implementation.
Website counters

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.