Php uses the CURL function to log on to the 163 mailbox and obtain its own address book.

Source: Internet
Author: User

I learned some basic knowledge about CURL and made this example. You can view the knowledge about CURL on the php official website and click here.
Sample code debugging method: replace $ userName and $ password with the userName and password of your 163 mailbox.
Note: the user name and password must be correct. Otherwise, an error is reported and no fault tolerance is performed.

The sample code is as follows:

The code is as follows: Copy code

<? Php
// =============================== Account information ==========================
// User name
$ UserName = 'xxxxxxxx ';
// Password
$ Password = 'xxxxxxxx ';
// Email
$ Email = $ userName. '@ 163.com ';
// ========================================================
// Logon address (the logon address is not the address set in the form. You have modified the form action attribute through js. You need to check the source code on the logon page to find the logon address)
$ LoginUrl = "https://ssl.mail.163.com/entry/coremail/fcg/ntesdoor2? Df = mail163_letter & from = web & funcid = loginone & iframe = 1 & language =-1 & passtype = 1 & product = mail163 & net = n & style =-1 & race = -2_56_-2 _ hz & uid = {$ email }";
// Post data sent during login (view form, note that there are hidden fields)
$ PostArray = array (
"Url2" => "http://mail.163.com/errorpage/error163.htm ",
"Savelogin" => 0, "username" => trim ($ userName), "password" => $ password,
);
$ PostString = '';
Foreach ($ postArray as $ key => $ value ){
$ PostString. = "{$ key} = {$ value }&";
}
$ PostString = trim ($ postString ,'&');
// Initialize the CURL object
$ Curl = curl_init ();
// Set the request address
Curl_setopt ($ curl, CURLOPT_URL, $ loginUrl );
// After being disabled, the CURL will be terminated from the server for verification.
Curl_setopt ($ curl, CURLOPT_SSL_VERIFYPEER, FALSE );
// When enabled, the obtained information is returned in the form of a file stream, rather than being output directly.
Curl_setopt ($ curl, CURLOPT_RETURNTRANSFER, TRUE );
// When enabled, the header file information is output as a data stream.
Curl_setopt ($ curl, CURLOPT_HEADER, TRUE );
// Set the POST parameter
Curl_setopt ($ curl, CURLOPT_POST, TRUE );
Curl_setopt ($ curl, CURLOPT_POSTFIELDS, $ postString );
// Execute the specified CURL session
// Returns TRUE if the call succeeds, and FALSE if the call fails.
// However, if the CURLOPT_RETURNTRANSFER option is set, the execution result is returned when the function is successfully executed, and FALSE if the function fails.
$ Html = curl_exec ($ curl );
// Write the obtained data to a file for viewing
// File_put_contents('temp1.txt ', $ html );
// Split the header file and content
List ($ head, $ content) = explode ("\ r \ n", $ html, 2 );
// Write the obtained data to a file for viewing
// File_put_contents('temp2.txt ', $ head );
// File_put_contents('temp3.txt ', $ content );
$ Head = explode ("\ r \ n", $ head );
// Obtain cookie information
$ CookieString = '';
Foreach ($ head as $ value ){
If (stripos ($ value, "Set-Cookie :")! = False ){
$ CookieString. = str_replace ("Set-Cookie:", "", $ value );
    }
}
// Analyze the sid value from the content (read the parameters of address book information)
$ StartString = 'Top. location. href = "';
$ EndString = '"; </script> ';
$ Start = strpos ($ content, $ startString );
$ End = strpos ($ content, $ endString );
$ TempUrl = substr ($ content, $ start + strlen ($ startString), $ end-$ start-strlen ($ startString ));
$ TempUrlVals = parse_url ($ tempUrl );
Parse_str ($ tempUrlVals ['query'], $ queryVals );
$ Sid = $ queryVals ['Sid '];
// ============================== Read email ==============================
// Read the email address
$ ReadUrl = "http://twebmail.mail.163.com/contacts/call.do? Uid = {$ email} & sid = {$ sid} & from = webmail & cmd = newapi. getContacts & vcardver = 3.0 & ctype = all & attachinfos = yellowpage ";
// Set the request address
Curl_setopt ($ curl, CURLOPT_URL, $ readUrl );
// Set the POST parameter
Curl_setopt ($ curl, CURLOPT_POST, TRUE );
Curl_setopt ($ curl, CURLOPT_POSTFIELDS, 'Order = [{"field": "N", "desc": "false"}] ');
// Set the cookie obtained from the logon operation.
Curl_setopt ($ curl, CURLOPT_COOKIE, $ cookieString );
// Disable header file output
Curl_setopt ($ curl, CURLOPT_HEADER, FALSE );
// Execute the specified CURL session
// Returns TRUE if the call succeeds, and FALSE if the call fails.
// However, if the CURLOPT_RETURNTRANSFER option is set, the execution result is returned when the function is successfully executed, and FALSE if the function fails.
$ Content = curl_exec ($ curl );
// Write the obtained data to a file for viewing
// File_put_contents('temp4.txt ', $ content );
// Close a CURL session and release resources
Curl_close ($ curl );
Echo '<pre> ';
Print_r (json_decode ($ content, true ));
Echo '</pre> ';

Example 2,You can enter more advanced information.

The code is as follows: Copy code

<Html>
<Head>
<Meta content = "text/html; charset = utf-8" http-equiv = "Content-Type"/>
<Title> log on to the 163 mailbox </title>
</Head>
<Body>
<Form id = "login" action = "mail. php" method = "POST">
<Label for = "username"> user name: </label> <input id = "username" name = "username" type = "text"/>
<Label for = "password"> password: </label> <input id = "password" name = "password" type = "password"/>
<Input type = "submit" value = "retrieve address book list"/>
</Form>
</Body>
</Html>

Mail. php

The code is as follows: Copy code

<? Php
Define ("COOKIEJAR", ini_get ("upload_tmp_dir"). DIRECTORY_SEPARATOR. "cookie.txt ");
   
If (! Isset ($ _ POST ['username']) |! Isset ($ _ POST ['username']) {
Echo 'error QUEST ';
Exit ();
    }
   
$ Username = $ _ POST ['username'];
$ Pos = strrpos ($ username ,'@');
If ($ pos = false ){
$ Username. = "@ 163.com ";
    }
$ Password = $ _ POST ['password'];
   
// Log on to the 163 mailbox and obtain the cookie after login
$ Options = array (
CURLOPT_URL => "https://reg.163.com/logins.jsp ",
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => "username =". $ username. "& password =". $ password,
CURLOPT_COOKIEJAR => COOKIEJAR,
);
Curl_quest ($ options );
   
// Use the cookie obtained in the previous step to obtain the cookie.
$ Options2 = array (
CURLOPT_URL => "http://fm163.163.com/coremail/fcg/ntesdoor2? Verifycookie = 1 & lightweight = 1 ",
CURLOPT_COOKIEFILE => COOKIEJAR,
CURLOPT_COOKIEJAR => COOKIEJAR,
);
Curl_quest ($ options2 );
   
// Analyze the cookie file and obtain coremail.
$ Cookiefile = file_get_contents (COOKIEJAR );
Preg_match ('| Coremail .*? % (\ S *) | ', $ cookiefile, $ sid );
   
// Send an xml request to obtain the address book.
$ PostStr3 = '<? Xml version = "1.0"?> <Object> <array name = "items"> <object> <string name = "func"> pab: searchContacts </string
> <Object name = "var"> <array name = "order"> <object> <string name = "field"> FN </string> <boolean name = "ignoreCase"
> True </boolean> </object> </array> </object> <string name = "func"> user: getSignatures </string
> </Object> <string name = "func"> pab: getAllGroups </string> </object> </array> </object> ';
$ Options3 = array (
CURLOPT_URL => 'http: // eg1a120.mail.163.com/a/s? Sid = '. $ sid [1].' & func = global: sequential ',
CURLOPT_HTTPHEADER => array ('content-Type: application/XML '),
CURLOPT_POST => 1,
CURLOPT_COOKIEFILE => COOKIEJAR,
CURLOPT_POSTFIELDS => $ postStr3,
CURLOPT_REFERER => 'http: // eg1a120.mail.163.com/a/f/js3/0811050934/index_v12.htm ',
);
$ Mailsxml = curl_quest ($ options3, true );
   
// Output the obtained address book xml
Header ("Content-Type: application/xml ");
Echo $ mailsxml;
   
Function curl_quest ($ ops, $ return = false ){
$ Ch = curl_init ();
Curl_setopt_array ($ ch, $ ops );
Ob_start ();
Curl_exec ($ ch );
If ($ return ){
$ Content = ob_get_contents ();
        }
Curl_close ($ ch );
Ob_end_clean ();
If ($ return ){
Return $ content;
        }
    }

?>

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.