PHP Curl Login to 163 mailbox and crawl mailbox buddy list code (tested)
Last Update:2017-02-28
Source: Internet
Author: User
Curl Technology is the simulation of the browser's action to achieve a page crawl or form submission, through this technology can achieve a lot of functions to go.
Copy Code The code is as follows:
<?php
error_reporting (0);
Mailbox username (with no @163.com suffix)
$user = ' papatata_test ';
Mailbox password
$pass = ' 000000 ';
Target Mailboxes
$mail _addr = Uenucom@163.com ';
Landing
$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 ';
$ch = Curl_init ($url);
Create a temporary file for storing cookie information
$cookie = Tempnam ('. ', ' ~ ');
$referer _login = ' http://mail.163.com ';
The return result is stored in a variable instead of the default direct output
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, $referer _login);
$fields _post = Array (
' username ' => $user,
' Password ' => $pass,
' Verifycookie ' =>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/3.0 ',
' 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 you close the connection, save the cookie returned from the server side 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/3.0 '
);
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);
Send the previously saved cookie information to the server side
curl_setopt ($ch, Curlopt_cookiefile, $cookie);
curl_setopt ($ch, Curlopt_cookiejar, $cookie);
$result = curl_exec ($ch);
Curl_close ($ch);
Get SID
Preg_match ('/sid=[^\ '].*/', $result, $location);
$sid = substr ($location [0], 4,-1);
File_put_contents ('./result.txt ', $sid);
Address List
$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/3.0 '
);
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, $cookie);
curl_setopt ($ch, Curlopt_cookiejar, $cookie);
$result = curl_exec ($ch);
Curl_close ($ch);
File_put_contents ('./result.txt ', $result);
Unlink ($cookie);
Start crawling 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);
?>