PHP Curl Login to 163 mailbox and crawl mailbox buddy list code (tested)

Source: Internet
Author: User
Tags array curl mail
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 CodeThe 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[^>]*> (. *?) &LT;\/A&GT;&LT;\/TD&GT;&LT;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.