CURL is a must-have killing medicine for home travel. Why? It is because it is easy to use to achieve the page capture simulation login collection and other 1 series of functions. Remember that the first time you touch CURL, you want to complete fetching from the list of mailbox users. At that time, in order to catch up with the schedule, I did not study carefully. I just found some information on the Internet and realized the function. Now the original code clean 1 function will still work
<?php
error_reporting ( 0 );
set_time_limit ( 0 );
header ( "Content-Type: text/html; charset=GB2312" );
// Email username and password
$user = 'username';
$pass = 'password';
// create 1 Three files for storage 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' );
// The 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 );
// achieve 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 scraping content
preg_match_all ( '/<td class="Ibx_Td_addrName"><a[^>]*>(.*?)<\/a><\/td><td class="Ibx_Td_addrEmail"><a[^>]*>(.*?)<\/a><\/td>/i', $result, $infos, PREG_SET_ORDER );
//1 Name: 2 : the email address
print_r ( $infos );
?>
Set up an PHP file to copy and save the above code, the effect will be immediate, remember to change the email account and password, the account does not need the @ suffix. CURL’s first experience, how is it? It’s not bad. Later, I saw another post on CSDN asking a question about getting express query. He wanted to put the query business of some large express companies in one page, which was indeed a very good practical gadget. However, because of the verification code for express query, it reminded me of CURL. Later, I helped the post master to realize the function. The idea was very simple. First, CURL was used to simulate fetching captchas and then displayed in the user submission page.
The source code is as follows: -getEms.html
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>EMS Express query </title>
</head>
<body>
<?php
fclose(fopen('cookie.txt','w')); // file cookie.txt Used to store the obtained cookie
$cookiejar = realpath('cookie.txt');
$fp = fopen("example_homepage.txt", "w"); // file example_homepage.txt Used to store the retrieved 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 fetch cookie
//readfile("example_homepage.jpg"); // View the retrieved image
?>
<form action="getems.php" method="post" name="form1">
The express no. : <input name="mailNum" type="text" value="EA739701017CS" />(13 position Fore and aft 2 The bits are letters )
<input name="code" type="text" value="" />
<?php echo "<img src='example_homepage.txt'>";?>
<input type="submit" value=" submit ">
</form>
</body>
</html>
-getems.php
<?php
if($_POST){
// Using the previous captcha cookie file
$cookiejar = realpath('cookie.txt');
// To obtain myEmsbarCode No. And the captcha 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);
// done
var_dump($_source);
exit;
}
?>
For the CURL library parameter details, there are many on the web that I have directly included List of Functions CURL library 1 has 17 functions: curl_close: Close the CURL session curl_copy_handle: Copies 1 CURL session handle and 3 copies all its parameters curl_errno: Returns the last error code curl_error: Returns a string describing the last error in the current session curl_exec: Executes the current session curl_getinfo: Gets specific information curl_init: Initializes the CURL session curl_multi_add_handle: Add 1 handle to 1 multi-connection session curl_multi_close: Closes 1 multi-handle CRUL session curl_multi_exec: Executes 1 multi-handle CURL session curl_multi_getcontent: Returns 1 handle executed if CURLOPT_RETURNTRANSFER is set curl_multi_info_read: Gets information about all current connections curl_multi_init: Initializes a multi-handle session curl_multi_remove_handle: Removes a handle from a multi-handle session curl_multi_select: Gets all bound sockets curl_setopt: Sets the CURL transport options curl_version: Gets the CURL version Commonly used to set options Boolean value options CURLOPT_AUTOREFERER: Automatically sets the forward connection when the returned header contains turn information CURLOPT_BINARYTRANSFER: TRUEtoreturntherawoutputwhenCURLOPT_RETURNTRANSFERisused. CURLOPT_COOKIESESSION: Flags the new cookie session, ignoring the cookie session set earlier CURLOPT_CRLF: Converts the line break of the Unix system to the Dos line break CURLOPT_DNS_USE_GLOBAL_CACHE: Use the global DNS cache CURLOPT_FAILONERROR: Return error is ignored CURLOPT_FILETIME: Gets the modification date of the request document, which can be obtained with curl_getinfo(). CURLOPT_FOLLOWLOCATION: Follows all redirection information returned by the server CURLOPT_FORBID_REUSE: Forces the session to close after the process has finished processing 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 in the output returned CURLOPT_HTTPGET: Set HTTP request mode to GET CURLOPT_HTTPPROXYTUNNEL: A connection is established via an HTTP agent CURLOPT_NOBODY: The output returned does not contain document information. CURLOPT_NOPROGRESS: Process level transfers are disabled. PHP is automatically set to true CURLOPT_NOSIGNAL: Ignores all messages sent to PHP CURLOPT_POST: The POST format is set to application/ ES174en-ES175en-ES176en-ES177en CURLOPT_PUTTRUE: Set PUT to upload files, and set CURLOPT_INFILE and CURLOPT_INFILESIZE CURLOPT_RETURNTRANSFER: Returns a string instead of a direct output after calling curl_exec() CURLOPT_SSL_VERIFYPEER: SSL validation enabled CURLOPT_UNRESTRICTED_AUTH: 1 Attach the username and password after the direct link and set CURLOPT_FOLLOWLOCATION CURLOPT_UPLOAD: Ready to upload the integer value option CURLOPT_BUFFERSIZE: Cache size CURLOPT_CONNECTTIMEOUT: Connection time setting, default 0 is unlimited CURLOPT_DNS_CACHE_TIMEOUT: Time to store DNS information in memory, default 2 minutes CURLOPT_INFILESIZE: The size of the file 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: * * * * Verification method CURLOPT_PROXYPORT: * * * * port CURLOPT_PROXYTYPE: * * * * type CURLOPT_TIMEOUT: Maximum execution time string option for the CURL function CURLOPT_COOKIE: cookie information in ES257en-cookie header CURLOPT_COOKIEFILE: The file that contains cookie information. The cookie file can be either Netscape or just the HTTP header CURLOPT_COOKIEJAR: The file that saves the cookie information after the connection ends CURLOPT_CUSTOMREQUEST: Custom request headers, using relative addresses CURLOPT_ENCODING: The value of ES278en-ES279en in the HTTP request header CURLOPT_POSTFIELDS: Data content submitted in POST format CURLOPT_PROXY: Proxy channel CURLOPT_PROXYUSERPWD: Agent authenticates 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 can be set in curl_init() CURLOPT_USERAGENT: Value of ES306en-ES307en in the HTTP header CURLOPT_USERPWD: Array of validation information options used by connection types CURLOPT_HTTP200ALIASES: 200 response code array. Is the response in the array considered to be the correct response CURLOPT_HTTPHEADER: Option to customize request header information to be a stream handle only: CURLOPT_FILE: Transmits the evening handle to be written. The default is standard output CURLOPT_INFILE: Transfers the file handle to read CURLOPT_STDERR: A replacement option for standard error output CURLOPT_WRITEHEADER: File callback option to write to transfer header information CURLOPT_HEADERFUNCTION: Callback function with two arguments, the first is the session handle and the second is the string of HTTP response header information. Using this callback function, the response header information is processed by itself. The response header information is returned on a line. Sets the return value to the length of the string. CURLOPT_READFUNCTION: Callback function with two arguments, the first is the session handle and the second is the string of HTTP response header information. Using this function, the returned data is processed by itself. The return value is the data size. CURLOPT_WRITEFUNCTION: Callback function with two arguments, the first is the session handle and the second is the string of HTTP response header information. Using this callback function, the response header information is processed by itself. The response header is the entire string. Sets the return value to the length of the string. 1 other examples of CURL (from the Web)
/*
* judge 1 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';}
/ Examples of asynchronous requests:
$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) ;
// Upload a file
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,'http://lh.tom.com/deal/import.php');
$fields = array(
'tname' => ' Moral by ',
'country' => 1,
'author' => ' Lao tze ',
'tags' => ' Moral by ',
'desc' => ' Tao can be tao, very Tao. It's a name, it's a name. The beginning of unknown heaven and earth. There is a mother of all things. Therefore, there is no desire to see the beauty. There is always a desire to see it. The two with different names, with the mystery. The door of mystery and mystery. ',
'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);
// Multi-file upload
$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 master php curl library you can do a lot of things you want to do, ha ha, not long ago to play happy net X world, the battle is really tedious, I directly wrote a combat assistant is very easy to use, this code is not open source :) master principle 1 kind of open source implementation. Website counter