A detailed _php instance of getting an email address based on PHP curl

Source: Internet
Author: User
Tags file copy readfile response code set cookie
Curl is a must-have killer for home travel. It's because he's easy to use. It can achieve a series of functions such as page capture simulation login collection.
Remember the first time you touch curl is to achieve the completion from the mailbox user list crawl. At that time in order to catch up without careful study just online to find some information to achieve the function. Now the original code to tidy up the function can still be used
Copy CodeThe code is as follows:
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 hold 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
$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><\/td>]*> (. *?) <\/a><\/td>/i ', $result, $infos, Preg_set_order);
1: Name 2: Email
Print_r ($infos);
?>

Create a php file copy the above code saved immediately after the effect, remember to change the mailbox account and password, the account does not need the @ suffix. Curl first experience, how, not bad.
Later on in the CSDN and see others posted asked a query to get express queries, he wants to put some big Express Company query business to do in a page, is indeed a very good practical gadgets, but because the express query has a verification code, not from the I think of Curl sharp weapon. Later to help the main implementation of the function, the idea is simple, first use curl simulation to crawl the verification code, and then display to the user submission page, while saving the verification code of the cookie and other user queries together to ensure that the cookie synchronization.

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



<title>EMS Express Query</title>


Fclose (fopen (' Cookie.txt ', ' W ')); File cookie.txt for storing the cookie obtained
$cookiejar = Realpath (' cookie.txt ');
$fp = fopen ("Example_homepage.txt", "w"); File example_homepage.txt for storing the content of the page obtained
$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 cookie that was taken
ReadFile ("example_homepage.jpg"); To view the pictures taken
?>





-getems.php
Copy CodeThe code is as follows:
if ($_post) {
Cookie file using a previous verification code
$cookiejar = Realpath (' cookie.txt ');
Get the Myemsbarcode number and Authenticode variable names
$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);

Get
Var_dump ($_source);
Exit
}
?>

For the Curl library parameters in detail, there are many online I directly included
List of functions There are 17 functions in the Curl Library:
Curl_close: Turn off 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: Executing the current session
Curl_getinfo: Get Specific information
Curl_init: Initializing a Curl session
Curl_multi_add_handle: Adding a handle to a multi-connection session
Curl_multi_close: Closing a multi-handle Crul session
Curl_multi_exec: Performing a multi-handle curl session
Curl_multi_getcontent: Returns the contents of a handle after execution if Curlopt_returntransfer is set
Curl_multi_info_read: Get information on all current connections
Curl_multi_init: Initializing a multi-handle session
Curl_multi_remove_handle: Removing a handle from a multi-handle session
Curl_multi_select: Get all bound sockets
curl_setopt: Setting Curl Transfer options
Curl_version: Get the Curl version
Common Settings Options Boolean options
Curlopt_autoreferer: Automatically sets the forward connection when the returned information header contains the steering information
Curlopt_binarytransfer:truetoreturntherawoutputwhencurlopt_returntransferisused.
Curlopt_cookiesession: Flag for new Cookie session, ignoring previously set cookie session
CURLOPT_CRLF: Converting a line break from a UNIX system to a DOS line break
Curlopt_dns_use_global_cache: Using the global DNS cache
Curlopt_failonerror: Ignore return error
Curlopt_filetime: Gets the modified date of the requested document, which can be obtained with curl_getinfo ().
Curlopt_followlocation: All redirection information returned by the server immediately
Curlopt_forbid_reuse: Forces the session to close after processing is finished and is no longer cached for reuse
Curlopt_fresh_connect: Forces a new session to be created instead of reusing the cached session
Curlopt_header: Contains the response header information in the returned output
Curlopt_httpget: Set HTTP request mode to get
Curlopt_httpproxytunnel: Establishing a connection via an HTTP proxy
Curlopt_nobody: The returned output does not contain document information.
Curlopt_noprogress: Prohibit process level transfer, PHP automatically set to True
Curlopt_nosignal: Ignore all messages sent to PHP
Curlopt_post: Set post mode to submit data, post format is application/x-www-form-urlencoded
Curlopt_puttrue: Set the put mode to upload files while setting curlopt_infile and Curlopt_infilesize
Curlopt_returntransfer: Returns a string instead of calling Curl_exec () directly after the output
CURLOPT_SSL_VERIFYPEER:SSL Authentication on
Curlopt_unrestricted_auth: Always link to append user name and password, and set curlopt_followlocation
Curlopt_upload: Ready to upload integer value option
Curlopt_buffersize: Cache Size
Curlopt_connecttimeout: Connection time setting, default 0 is unlimited
Curlopt_dns_cache_timeout: Time to save DNS information in memory, default 2 minutes
Curlopt_infilesize: File size uploaded to the 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 number of turns
Curlopt_port: Connection Port
curlopt_proxyauth:***** authentication method
curlopt_proxyport:***** Port
curlopt_proxytype:***** type
The maximum execution time string option for the Curlopt_timeout:curl function
COOKIE information in Set-cookie in curlopt_cookie:http header
Curlopt_cookiefile: File containing cookie information, the format of the cookie file can be in Netscape format, or just the format of the HTTP header
Curlopt_cookiejar: file that stores cookie information after connection ends
Curlopt_customrequest: Custom request header, using relative address
Curlopt_encoding:http the value of accept-encoding in the request header
Data content submitted in curlopt_postfields:post format
Curlopt_proxy: Proxy Channel
CURLOPT_PROXYUSERPWD: Proxy authentication user name and password
Curlopt_range: Returns the range of data, in bytes
Curlopt_referer: Forward Link
Curlopt_url: The URL address to connect to, which can be set in Curl_init ()
Value of User-agent in Curlopt_useragent:http header
CURLOPT_USERPWD: Array options for authentication information used by connection species
CURLOPT_HTTP200ALIASES:200 Response Code Array, the response in the array is considered to be the correct response
Curlopt_httpheader: The custom request header information can only be an option for a flow handle:
Curlopt_file: Transmission of the evening handle to be written, default is standard output
Curlopt_infile: Transfer the file handle to be read
Curlopt_stderr: As a replacement option for standard error output
Curlopt_writeheader: Transfer header information to write the file callback function option
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. With 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. With this function, the returned data is processed on its own. The return value is the data size.
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. With 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.
Some other examples of curl (excerpt from the web)
Copy CodeThe code is 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 = ' http://testpic1.tomoimg.cn/240x180/394/855/517932781/200901/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 Sutra ',
' Country ' = 1,
' Author ' = ' Lao Tzu ',
' Tags ' = ' moral Sutra ',
' desc ' = ' and ' way, very way. The name is name, very famous. The beginning of the nameless world. The mother of all things. Therefore, we often have no desire to view its wonderful. Often desire to view its total self abandonment. The two are different names, the same as the Xuan. Convoluted, the gate of all the wonderful. ',
' 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, hehe, not long ago to play the X world of happy, fighting is really cumbersome, I directly wrote a combat assistant very useful, this code is not open Source:) Master principle as open source implementation.
Website 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.