我们使用phpmailer登录邮件发邮件也是使用了curl原理来实现模仿用户(www.phpfensi.com)发邮件了,今天看了两个利用CURL函数登入163邮箱并获取自己的通讯录的例子.
学习了一些CURL的基础知识并做了这个示例,关于CURL的知识可以从php的官网上查看,点击查看.
示例代码调试方式:把$userName和$password换成自己163邮箱的用户名和密码即可.
注意:用户名和密码一定要正确,否则报错,没有做容错处理,示例代码如下:
- <?php
-
-
- $userName = 'xxxxxxxx';
-
- $password = 'xxxxxxxx';
-
- $email = $userName . '@163.com';
-
-
- $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}";
-
- $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, '&');
-
- $curl = curl_init();
-
- curl_setopt($curl, CURLOPT_URL, $loginUrl);
-
- curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
-
- curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
-
- curl_setopt($curl, CURLOPT_HEADER, TRUE);
-
- curl_setopt($curl, CURLOPT_POST, TRUE);
- curl_setopt($curl, CURLOPT_POSTFIELDS, $postString);
-
-
-
- $html = curl_exec($curl);
-
-
-
- list($head, $content) = explode("\r\n\r\n", $html, 2);
-
-
-
- $head = explode("\r\n", $head);
-
- $cookieString = '';
- foreach ($head as $value){
- if(stripos($value, "Set-Cookie: ") !== false){
- $cookieString .= str_replace("Set-Cookie: ", "", $value);
- }
- }
-
- $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'];
-
-
- $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";
-
- curl_setopt($curl, CURLOPT_URL, $readUrl);
-
- curl_setopt($curl, CURLOPT_POST, TRUE);
- curl_setopt($curl, CURLOPT_POSTFIELDS, 'order=[{"field":"N","desc":"false"}]');
-
- curl_setopt($curl, CURLOPT_COOKIE, $cookieString);
-
- curl_setopt($curl, CURLOPT_HEADER, FALSE);
-
-
-
- $content = curl_exec($curl);
-
-
- (www.phpfensi.com)
- curl_close($curl);
- echo '<pre>';
- print_r(json_decode($content, true));
- echo '</pre>';
例子二,这个更高级一些可以输入信息,代码如下:
- <html>
- <head>
- <meta content="text/html; charset=utf-8" http-equiv="Content-Type"/>
- <title>登陆163邮箱</title>
- </head>
- <body>
- <form id="login" action="mail.php" method="POST">
- <label for="username">用户名:</label><input id="username" name="username" type="text" />
- <label for="password">密码:</label><input id="password" name="password" type="password" />
- <input type="submit" value="取得通讯录列表" />
- </form>
- </body>
- </html>
mail.php,代码如下:
- <?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'];
-
-
- $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);
-
-
- $options2 = array(
- CURLOPT_URL => "http://fm163.163.com/coremail/fcg/ntesdoor2?verifycookie=1&lightweight=1",
- CURLOPT_COOKIEFILE => COOKIEJAR,
- CURLOPT_COOKIEJAR => COOKIEJAR,
- );
- curl_quest($options2);
-
-
- $cookiefile = file_get_contents(COOKIEJAR);
- preg_match('|Coremail.*?%(\S*)|',$cookiefile,$sid);
-
-
- $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></object><object><string name="func">user:getSignatures</string
- ></object><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);
-
-
- 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;
- }
- }
-
- ?>
|