A detailed _php instance of obtaining the email address based on PHP curl

Source: Internet
Author: User
Tags curl error code file copy http request readfile response code set cookie email account
Curl is a necessary medicine for home travel, why so described? Because he is easy to use and convenient to achieve a page crawl analog login collection and other functions.
Remember the first contact curl is to achieve completion from the mailbox user list crawl. At that time in order to catch up on the progress of the study is only online to find some information to achieve the function. Now the original code to clean up the function can still be used
Copy Code code as follows:

<?php
error_reporting (0);
Set_time_limit (0);
Header ("content-type:text/html; charset=gb2312 ");

Mailbox User name 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 List
$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 ('/<td class= "Ibx_td_addrname" ><a[^>]*> (. *?) &LT;\/A&GT;&LT;\/TD&GT;&LT;TD class= "Ibx_td_addremail" ><a[^>]*> (. *?) <\/a><\/td>/i ', $result, $infos, Preg_set_order);
1: Name 2: Email
Print_r ($infos);
?>

Create a php file copy the above code saved after the effect is immediate, remember to change the email account and password, the account does not need the @ suffix. Curl first experience, how, it's not bad.
Later on the CSDN and see others post ask a Get express query problem, he wants to send some big Express company inquires the business to do in a page, really is a very good practical gadget, but because the express inquiry has the verification code, does not let me think of the Curl sharp weapon. Later, the main implementation of the function, the idea is very simple, first with Curl simulation crawl verification code, and then display to the user submission page, while saving the authentication code cookies and other user queries submitted together to ensure that the cookie synchronization.

The source code is as follows:
-getems.html
Copy Code code as follows:

<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 ">
<title>ems Express Inquiry </title>
<body>
<?php
Fclose (fopen (' Cookie.txt ', ' W ')); File Cookie.txt for storing the obtained cookies
$cookiejar = Realpath (' cookie.txt ');
$fp = fopen ("Example_homepage.txt", "w"); File Example_homepage.txt 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); To view the cookies that are fetched
ReadFile ("example_homepage.jpg"); View a picture taken
?>
<form action= "getems.php" method= "Post" Name= "Form1" >
Courier Number: <input name= "Mailnum" type= "text" value= "Ea739701017cs"/> (13-digit 2 digits are all letters)
<input name= "Code" type= "text" value= ""/>
<?php echo "
<input type= "Submit" value= "submitted" >
</form>

</body>

-getems.php
Copy Code code as follows:

<?php
if ($_post) {
Cookie file with previous authentication code
$cookiejar = Realpath (' cookie.txt ');
Get the Myemsbarcode number and the validation code variable name
$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 ("/<input type=\" hidden\ "name=\" Myemsbarcode\ "value=\" (. *) \ "\/>/isu", $result, $myEmsbarCode);
Preg_match ("/<\/span><input name=\" (. *) \ "type=\" text\ "/isu", $result, $codename);

$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);

Get
Var_dump ($_source);
Exit
}
?>

For the Curl library parameters detailed, online there are a lot of my direct collection of
Function List Curl Library A total of 17 functions:
Curl_close: Close Curl Session
Curl_copy_handle: Copy a curl session handle, while 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: Executing current session
Curl_getinfo: Getting Specific information
Curl_init: Initializing Curl session
Curl_multi_add_handle: Add a handle to a multiple-connection session
Curl_multi_close: Close a multiple handle Crul session
Curl_multi_exec: Execute a multiple handle curl session
Curl_multi_getcontent: Returns the contents of a handle after execution if the Curlopt_returntransfer is set
Curl_multi_info_read: Get information for all current connections
Curl_multi_init: Initialize a multiple handle session
Curl_multi_remove_handle: Removes a handle from a multiple-handle session
Curl_multi_select: Get all bound sockets
curl_setopt: Set Curl transfer options
Curl_version: Get Curl Version
Common Setting Options Boolean options
Curlopt_autoreferer: Automatically set forward connection when the returned information header contains steering information
Curlopt_binarytransfer:truetoreturntherawoutputwhencurlopt_returntransferisused.
Curlopt_cookiesession: Flag for new Cookie session, ignore previously set cookie session
Curlopt_crlf: Converts a newline character from a Unix system to a DOS newline character
Curlopt_dns_use_global_cache: Use global DNS cache
Curlopt_failonerror: Ignore return error
Curlopt_filetime: Gets the modified date of the request document, which can be obtained using Curl_getinfo ().
Curlopt_followlocation: Follow all redirect information returned by the server
Curlopt_forbid_reuse: Forces the session to shut down after the process has been processed and is no longer cached for reuse
Curlopt_fresh_connect: Force a new session to be created instead of reusing a cached session
Curlopt_header: Include the response header information in the returned output
Curlopt_httpget: Set HTTP request mode to get
Curlopt_httpproxytunnel: Connecting via an HTTP proxy
Curlopt_nobody: The returned output does not contain document information.
Curlopt_noprogress: Disable process level transfer, PHP automatically set to True
Curlopt_nosignal: Ignore all information sent to PHP
Curlopt_post: Set post mode submit data, post format is application/x-www-form-urlencoded
Curlopt_puttrue: Set the put way to upload files while setting up Curlopt_infile and Curlopt_infilesize
Curlopt_returntransfer: Returns a string instead of calling curl_exec () directly output
Curlopt_ssl_verifypeer:ssl Verify Open
Curlopt_unrestricted_auth: Always attach user name and password after link, and set curlopt_followlocation
Curlopt_upload: Ready to upload integer value option
Curlopt_buffersize: Cache Size
Curlopt_connecttimeout: Connection time setting, default 0 is unrestricted
Curlopt_dns_cache_timeout: Time to save DNS information in memory, default 2 minutes
Curlopt_infilesize: File size for upload to remote site
Curlopt_low_speed_limit: Transmission minimum speed limit andabort.
Curlopt_low_speed_time: Transmission time limit
Curlopt_maxconnects: Maximum number of persistent connections
Curlopt_maxredirs: Maximum Steering number
Curlopt_port: Connection Port
curlopt_proxyauth:***** authentication method
curlopt_proxyport:***** Port
curlopt_proxytype:***** type
Maximum execution time string option for the Curlopt_timeout:curl function
COOKIE information in the Set-cookie in the curlopt_cookie:http header
Curlopt_cookiefile: File containing cookie information, the format of the cookie file can be in Netscape format, or just the HTTP header format
Curlopt_cookiejar: A file that stores cookie information after the connection is completed
Curlopt_customrequest: Custom request headers, using relative addresses
Value of accept-encoding in Curlopt_encoding:http request header
Data content submitted in curlopt_postfields:post format
Curlopt_proxy: Proxy Channel
CURLOPT_PROXYUSERPWD: Proxy authenticated username and password
Curlopt_range: Returns the range of data, in bytes
Curlopt_referer: Forward Link
Curlopt_url: The URL address to be connected, which can be set in Curl_init ()
The value of the user-agent in the curlopt_useragent:http header
CURLOPT_USERPWD: Array options for authentication information used by the connection type
CURLOPT_HTTP200ALIASES:200 Response Code Array, the response in the array is considered to be the correct response
Curlopt_httpheader: Custom Request header information can only be an option for a stream handle:
Curlopt_file: Transfer the night handle to write, default is standard output
Curlopt_infile: Transfer file handle to read
Curlopt_stderr: A replacement option for standard error output
Curlopt_writeheader: Transmission header information file callback function options to write
Curlopt_headerfunction: A callback function with two parameters, the first is a session handle, and the second is a string of HTTP response header information. Using this callback function, the response header information is processed on its own. The response header information is returned by row. Sets the return value to the string length.
Curlopt_readfunction: A callback function with two parameters, the first is a session handle, and the second is a string of HTTP response header information. Using this function, the returned data is processed on its own. The return value is the data dimension.
Curlopt_writefunction: A callback function with two parameters, the first is a session handle, and the second is a string of HTTP response header information. Using this callback function, the response header information is processed on its own. The response header information is the entire string. Sets the return value to the string length.
Other examples of curl (excerpt from the network)
Copy Code code as follows:

/*
* Determine if 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 = ' yun_qi_img/12312215602409.jpg ';
if (Isrealurl ($url)) {echo ' Yes ';} Else{echo ' no ';}

/Asynchronous Request Example:
$userid = 517932781;
$imageid = 1520;
$albumid = 2637;
$tags = ' AA ';
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);

Uploading files
$ch = Curl_init ();
curl_setopt ($ch, Curlopt_url, ' http://lh.tom.com/deal/import.php ');
$fields = Array (
' Tname ' => ' moral scripture ',
' Country ' => 1,
' Author ' => ' Laozi ',
' Tags ' => ' moral Sutra ',
' desc ' => ' way, very way. Name, very name. The beginning of the nameless world. The mother of all things. So often there is no desire to view its wonderful. Often want to view its total self abandonment. The two are the same and different names, the same as the Xuan. Convoluted, the gate of the good. ',
' 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);

Multiple file uploads
$ch = Curl_init ();
curl_setopt ($ch, Curlopt_url, ' http://lh.tom.com/deal/addpic.php ');
$j = 0;
$fields = Array (
' Vid ' => 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 mastered the PHP Curl library You can do a lot of things you want to do, oh, before long play net of the X world, fighting is really cumbersome, I directly wrote a combat assistant very easy to use, this code is not open source: the principle of the same open source realization.
Web counter

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.