Php curl function introduction and capture 163 Mail List

Source: Internet
Author: User

If you see this, you need to set your php tutorial and enable this library. If you are on windows, it is very simple. You need to modify the settings of your php. ini file, find php_curl.dll, and cancel the semicolon comment. As follows:
// Cancel the comment
Extension = php_curl.dll

If you are under linux, You need to recompile Your php for google ranking. During editing, you need to open the compilation parameter -- add the "-with-curl" parameter to the configure command.
A small example
If everything is ready, the following is a small routine:
Copy the Code as follows:

<? Php
// Initialize a curl object
$ Curl = curl_init ();
// Set the url you want to capture
Curl_setopt ($ curl, curlopt_url, 'HTTP: // jb51.net ');
// Set the header
Curl_setopt ($ curl, curlopt_header, 1 );
// Set the curl parameter to save the result to the string or output to the screen.
Curl_setopt ($ curl, curlopt_returntransfer, 1 );
// Run curl to request the webpage
$ Data = curl_exec ($ curl );
// Close the url request
Curl_close ($ curl );
// Display the obtained data
Var_dump ($ data );

How to post data
The above is the code for capturing web pages, and the following is to post data to a Web page. Suppose we have a form processing URL http://www.example.com/sendsms.php, which can accept two single table domain, one is the phone number, and the other is the SMS content.
Copy the Code as follows:

<? Php
$ Phonenumber = '000000 ';
$ Message = 'this message was generated by curl and php ';
$ Curlpost = 'pnumber = '. urlencode ($ phonenumber).' & message = '. urlencode ($ message).' & submit = send ';
$ Ch = curl_init (); chain link fencing
Curl_setopt ($ ch, curlopt_url, 'HTTP: // www.example.com/sendsms.php ');
Curl_setopt ($ ch, curlopt_header, 1 );
Curl_setopt ($ ch, curlopt_returntransfer, 1 );
Curl_setopt ($ ch, curlopt_post, 1 );
Curl_setopt ($ ch, curlopt_postfields, $ curlpost );
$ Data = curl_exec ();
Curl_close ($ ch );
?>

From the above procedure, we can see that using curlopt_post to set the post method of the http protocol, instead of the get method, and then setting the post data with curlopt_postfields.
About proxy servers
The following is an example of how to use the proxy server. Pay attention to the highlighted Code. The Code is very simple and I don't need to talk about it.
Copy the Code as follows:

<? Php
$ Ch = curl_init ();
Curl_setopt ($ ch, curlopt_url, 'HTTP: // www.example.com ');
Curl_setopt ($ ch, curlopt_header, 1 );
Curl_setopt ($ ch, curlopt_returntransfer, 1 );
Curl_setopt ($ ch, curlopt_httpproxytunnel, 1 );
Curl_setopt ($ ch, curlopt_proxy, 'fakeproxy. com: 1080 ');
Curl_setopt ($ ch, curlopt_proxyuserpwd, 'user: password ');
$ Data = curl_exec ();
Curl_close ($ ch );
?>


  
About ssl and cookie
For ssl, that is, the https tutorial protocol, you only need to change http: // In the curlopt_url connection to https. Of course, the parameter curlopt_ssl_verifyhost can be set as a verification site.
For cookie, you need to understand the following three parameters:
Curlopt_cookie: Set a cookie in the face-to-face session.
Curlopt_cookiejar saves a cookie when the session ends.
Curlopt_cookiefile: the cookie file.
Http Server Authentication
Finally, let's take a look at the situation of http Server Authentication.
Copy the Code as follows:

<? Php
$ Ch = curl_init ();
Curl_setopt ($ ch, curlopt_url, 'HTTP: // www.example.com ');
Curl_setopt ($ ch, curlopt_returntransfer, 1 );
Curl_setopt ($ ch, curlopt_httpauth, curlauth_basic );
Curl_setopt (curlopt_userpwd, '[username]: [password]')
$ Data = curl_exec ();
Curl_close ($ ch );
?>


Check a code that uses curl to capture the 163 email address list

The curl technology is to simulate the action of a browser to capture pages or submit forms. This technology can be used to implement many de-reading functions.
Copy the Code as follows:

<? Php
Error_reporting (0 );
// Email user name (without the suffix @ 163.com)
$ User = 'papatata _ test ';
// Email Password
$ Pass = '000000 ';
// Target email address
// $ Mail_addr = uenucom@163.com ';
// Login
$ Url = 'HTTP: // reg.163.com/logins.jsptutorial? Type = 1 & url = http://entry.mail.163.com/coremail/fcg/ntesdoor2? Lightweight % 3d1% 26 verifycookie % 3d1% 26 language % 3d-1% 26 style % 3d-1 ';
$ Ch = curl_init ($ url );
// Create a temporary file for storing cookie Information
$ Cookie = tempnam ('.','~ ');
$ Referer_login = 'HTTP: // mail.163.com ';
// The returned result is stored in the variable instead of the default direct output.
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, $ referer_login );
$ Fields_post = array (
'Username' => $ user,
'Password' => $ pass,
'Verifycookies' => 1,
'Style' =>-1,
'Product' => 'mail163 ',
'Seltype' =>-1,
'Secure '=> 'on'
);
$ Headers_login = array (
'User-agent' => 'mozilla/5.0 (windows; u; windows nt 5.1; zh-cn; rv: 1.9) gecko/2008052906 firefox/123456 ',
'Referer' => 'HTTP: // www.163.com'
);
$ Fields_string = '';
Foreach ($ fields_post as $ key => $ value)
{
$ Fields_string. = $ key. '='. $ value .'&';
}
$ Fields_string = rtrim ($ fields_string ,'&');
Curl_setopt ($ ch, curlopt_cookiesession, true );
// When the connection is closed, the cookie returned by the server is saved in the following file:
Curl_setopt ($ ch, curlopt_cookiejar, $ cookie );
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 ';
$ Ch = curl_init ($ url );
$ Headers = array (
'User-agent' => 'mozilla/5.0 (windows; u; windows nt 5.1; zh-cn; rv: 1.9) gecko/2008052906 firefox/123456'
);
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 );
// Send the previously saved cookie information to the server together
Curl_setopt ($ ch, curlopt_cookiefile, $ cookie );
Curl_setopt ($ ch, curlopt_cookiejar, $ cookie );
$ Result = curl_exec ($ ch );
Curl_close ($ ch );
// Obtain the sid
Preg_match ('/sid = [^ "]. */', $ result, $ location );
$ Sid = substr ($ location [0], 4,-1 );
// File_put_contents ('./result.txt', $ sid );
// Address book
$ Url = 'HTTP: // g4a30.mail.163.com/jy3/address/addrlist.jsp? Sid = '. $ sid.' & gid = all ';
$ Ch = curl_init ($ url );
$ Headers = array (
'User-agent' => 'mozilla/5.0 (windows; u; windows nt 5.1; zh-cn; rv: 1.9) gecko/2008052906 firefox/123456'
);
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, $ cookie );
Curl_setopt ($ ch, curlopt_cookiejar, $ cookie );
$ Result = curl_exec ($ ch );
Curl_close ($ ch );
// File_put_contents ('./result.txt', $ result );
Unlink ($ cookie );
// Start capturing 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: Email
Print_r ($ infos );
?>

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.