php 模拟GMAIL,HOTMAIL(MSN),YAHOO,163,126邮箱登录的详细介绍


Posted in PHP onJune 18, 2013

最近一直在忙,赶在这个假期结束的时候,发表一下此文,为了是让这些源码开源出来
当然这些方法可能不可取,但大致应该是这种方向了吧,由于目前做的一个UCHOME港台的站点
咱们策划说要改一下好友邀请里面的显示方式,开始我也没有在意,当时就说行!
到了弄这个的时候才发现,UCH里面的这个地方是用漫游型式做的,让人很是无解,啥都改不了!
唉,既然都答应了说可以,现在实际情况不行了,所以觉得不好意思,为了快速解决这问题,在GOOGLE,
百度上搜了个遍,结果又出忽意料,就一个126邮箱的开源例子,其它的都没有,有一牛哥留着QQ说要其它的源码,
可以加QQ买!不对此人评论了!俺花了点时间,整了整,结果就幸运的弄出来了几个,因为时间有限,目前手上的项目一直在弄,所以就没有管其它的,现在给出 GMAIL,HOTMAIL(MSN),YAHOO的邮箱联系人的PHP源代码:
 
1.GMAIL

<?php
define( "COOKIEJAR", tempnam( ini_get( "upload_tmp_dir" ), "cookie" ) );   //定义COOKIES存放的路径,要有操作的权限
define( "TIMEOUT", 1000 ); //超时设定
class GMAIL
{
        private function login($username, $password)
        {               
                //第一步:模拟抓取登录页面的数据,并记下cookies
                $cookies = array();
                $matches = array();
                //获取表单
                $login_url = "https://www.google.com/accounts/ServiceLoginAuth";
                $ch = curl_init($login_url);                curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
                curl_setopt($ch, CURLOPT_COOKIEJAR, COOKIEJAR);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                $contents = curl_exec($ch);
                curl_close($ch);
                //模拟参数
                $name = array('dsh','timeStmp','secTok'); 
                foreach($name as $v) {
                     preg_match('/<input/s*type="hidden"/s*name="'.$v.'"/s*id="'.$v.'"/s*value="(.*?)"/s*//>/i', $contents, $matches);
                    if(!empty($matches)) {
                        $$v = $matches[1];
                        $matches = array();
                    }        
                }
                $server = 'mail';
                preg_match('/<input/s*type="hidden"/s*name="GALX"/s*value="(.*?)"/s*//>/i', $contents, $matches);
                if(!empty($matches)) {
                    $GALX = $matches[1];
                    $matches = array();
                } 
                $timeStmp = time();
                //第二步: 开始登录
                $ch = curl_init();
                curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
                curl_setopt($ch, CURLOPT_URL, "https://www.google.com/accounts/ServiceLoginAuth");
                curl_setopt($ch, CURLOPT_COOKIEFILE, COOKIEJAR); 
                curl_setopt($ch, CURLOPT_POST, 1);
                $fileds = "dsh=$dsh&Email=".$username."&Passwd={$password}&GALX=$GALX&timeStmp=$timeStmp&secTok=$secTok&signIn=Sign in&rmShown=1&asts=&PersistentCookie=yes"; 
                curl_setopt($ch, CURLOPT_POSTFIELDS, $fileds); 
                curl_setopt($ch, CURLOPT_COOKIEJAR, COOKIEJAR);              
                curl_setopt($ch, CURLOPT_TIMEOUT, TIMEOUT);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                $str = curl_exec($ch);               
                curl_close($ch);
                //第三步:check Cookies即也算是个引导页面
                $ch = curl_init("https://www.google.com/accounts/CheckCookie?chtml=LoginDoneHtml");
                curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                curl_setopt($ch,CURLOPT_COOKIEFILE,COOKIEJAR);
                curl_setopt($ch, CURLOPT_COOKIEJAR, COOKIEJAR);                 
                curl_setopt($ch, CURLOPT_TIMEOUT, TIMEOUT);     
                $str2 = curl_exec($ch);
                curl_close($ch);
                if (strpos($contents, "安全退出") !== false)
                {                        
                        return FALSE;
                }               
                return TURE;
        }
        //获取邮箱通讯录-地址
        public function getAddressList($username, $password)
        {               
                if (!$this->login($username, $password))
                {
                        return FALSE;
                }
                //开始进入模拟抓取
                $ch = curl_init();
                curl_setopt($ch, CURLOPT_URL, "http://mail.google.com/mail/contacts/data/contacts?thumb=true&groups=true&show=ALL&enums=true&psort=Name&max=300&out=js&rf=&jsx=true");  //out=js返回json数据,不设置返回为xml数据   
                curl_setopt($ch, CURLOPT_COOKIEFILE, COOKIEJAR);
                /*  对于返回xml数据时需要此设置
                curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/xml"));
                $str = "<?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>";
                curl_setopt($ch, CURLOPT_POSTFIELDS, $str);
                */
                curl_setopt($ch, CURLOPT_POST, 1);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                curl_setopt($ch, CURLOPT_TIMEOUT, TIMEOUT);
                $contents = curl_exec($ch);
                curl_close($ch);
                //die($contents);
                //get mail list from the page information username && emailaddress
                /* 对于返回xml数据时的处理
                preg_match_all("/<string/s*name=/"EMAILREF/">(.*)<//string>/Umsi",$contents,$mails);
                preg_match_all("/<string/s*name=/"FN/">(.*)<//string>/Umsi",$contents,$names);
                $users = array();
                foreach($names[1] as $k=>$user)
                {
                    //$user = iconv($user,'utf-8','gb2312');
                    $users[$mails[1][$k]] = $user;
                }
                if (!$users)
                {
                    return '您的邮箱中尚未有联系人';
                }  
                */ 
                $contents = substr($contents, strlen('while (true); &&&START&&&'),  -strlen('&&&END&&& ')); 
                return $contents;
        }
}
$gamil = new GMAIL;
$res = $gamil->getAddressList('username@163.com','123456');
echo $res;
?>
<mce:script type="text/javascript"><!--
var data = <?php echo $res; ?>
alert(data['Body']); //json数据
// --></mce:script>

2.HOTMAIL(MSN)
<?php
define( "COOKIEJAR", tempnam( ini_get( "upload_tmp_dir" ), "cookie" ) );   //定义COOKIES存放的路径,要有操作的权限
define( "TIMEOUT", 1000 ); //超时设定
class MSN
{
        function getAddressList($username, $password)
        {               
                //第一步:模拟抓取登录页面的数据,并记下cookies
                $cookies = array();
                $ch = curl_init();
                curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
                curl_setopt($ch, CURLOPT_URL, "http://login.live.com/login.srf?wa=wsignin1.0&rpsnv=11&ct=".time()."&rver=6.0.5285.0&wp=MBI&wreply=http:%2F%2Fmail.live.com%2Fdefault.aspx&lc=2052&id=64855&mkt=en");
                curl_setopt($ch, CURLOPT_COOKIEJAR, COOKIEJAR);
                curl_setopt($ch, CURLOPT_TIMEOUT, TIMEOUT);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                $str = curl_exec($ch);              
                curl_close($ch);
                //参数的分析
                $matches = array();       
                preg_match('/<input/s*type="hidden"/s*name="PPFT"/s*id="(.*?)"/s*value="(.*?)"/s*//>/i', $str, $matches); 
                $PPFT = $matches[2];                preg_match('/srf_sRBlob=/'(.*?)/';/i', $str, $matches); 
                $PPSX = $matches[1];
                $type = 11;
                $LoginOptions = 3;
                $Newuser = 1;
                $idsbho = 1;
                $i2 = 1;
                $i12 = 1;
                $i3 = '562390';
                $PPSX = 'Pa';
                //合并参数
                $postfiles = "login=".$username."&passwd=".$password."&type=".$type."&LoginOptions=".$LoginOptions."&Newuser=".$Newuser."&idsbho=".$idsbho."&i2=".$i2."&i3=".$i3."&PPFT=".$PPFT."&PPSX=".$PPSX."&i12=1";
                //第二步:开始登录
                $ch = curl_init();
                curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
                curl_setopt($ch, CURLOPT_COOKIEFILE, COOKIEJAR);
                curl_setopt($ch, CURLOPT_URL, 'https://login.live.com/ppsecure/post.srf?wa=wsignin1.0&rpsnv=11&ct='.(time()+5).'&rver=6.0.5285.0&wp=MBI&wreply=http:%2F%2Fmail.live.com%2Fdefault.aspx&lc=2052&id=64855&mkt=en&bk='.(time()+715)); //此处的两个time()是为了模拟随机的时间            
                curl_setopt($ch, CURLOPT_POST, 1);
                curl_setopt($ch, CURLOPT_POSTFIELDS, $postfiles);
                curl_setopt($ch, CURLOPT_COOKIEJAR, COOKIEJAR);
                //curl_setopt($ch, CURLOPT_HEADER, 1);
                curl_setopt($ch, CURLOPT_TIMEOUT, TIMEOUT);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                $content = curl_exec($ch);
                curl_close($ch);
                if( stripos($content,'WLWorkflow') !== FALSE ) {     //WLWorkflow登录页面JS
                    return false;      //登录失败
                }
                //获取location链接
                $matches = array();       
                preg_match('/window.location.replace/(/"(.*?)/"/)/i', $content, $matches); 
                $url_contiune_1 = $matches[1]; //接下来的链接
                if(!$url_contiune_1) {
                    return false;
                }
                //第三步: 进入引导页面
                $ch = curl_init();
                curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
                curl_setopt($ch, CURLOPT_COOKIEFILE, COOKIEJAR);
                curl_setopt($ch, CURLOPT_URL, $url_contiune_1);
                curl_setopt($ch, CURLOPT_COOKIEJAR, COOKIEJAR);
                curl_setopt($ch, CURLOPT_HEADER, 1); 
                curl_setopt($ch, CURLOPT_TIMEOUT, TIMEOUT);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                $content_2 = curl_exec($ch);
                //echo $postfiles;  
                curl_close($ch);
                //获取redicturl链接
                $matches = array();       
                preg_match('/<a/s*href=/"(.*?)/"/s*>/i', $content_2, $matches); 
                $url_contiune_2 = $matches[1]; //接下来的链接
                if(!$url_contiune_2) {
                    return false;
                }
                //跳过进入首页
                /*
                $ch = curl_init();
                curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
                curl_setopt($ch, CURLOPT_COOKIEFILE, COOKIEJAR);
                curl_setopt($ch, CURLOPT_URL, $url_contiune_2);
                curl_setopt($ch, CURLOPT_COOKIEJAR, COOKIEJAR);
                curl_setopt($ch, CURLOPT_HEADER, 1); 
                curl_setopt($ch, CURLOPT_TIMEOUT, 1000);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                $content_3 = curl_exec($ch);
                curl_close($ch);
                */
                //获取邮箱请求基址 读取host
                $matches = array();       
                preg_match('/(.*?)////(.*?)//(.*?)/i', $url_contiune_2, $matches); 
                $url_contiune_3 = trim($matches[1]).'//'.trim($matches[2]); //首页定义的站点基址
                $url_4 = $url_contiune_3.'/mail/ContactMainLight.aspx?n=435707983'; //n后面的数字是随机数
                if(!$url_contiune_3) {
                    return false;
                }
                //第四步: 开始获取邮箱联系人
                //base  $url_4
                $ch = curl_init();
                curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
                curl_setopt($ch, CURLOPT_COOKIEFILE, COOKIEJAR);
                curl_setopt($ch, CURLOPT_URL, $url_4);
                curl_setopt($ch, CURLOPT_TIMEOUT, TIMEOUT);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                $str = curl_exec($ch);
                //分析数据 (此处的数据因为hotmail的JS处理机制,所以在页面上看不出来,源码上可以看到数据)
                return $this->hanlde_date($str);
        }
        function hanlde_date($data) {
                $new_str = array();
                if(!empty($data)) {
                        $ops_start = stripos($data,'ic_control_data');
                        $ops_end = stripos($data,';',$ops_start);
                        $new_str = substr($data, $ops_start + strlen('ic_control_data = '), $ops_end - $ops_start - strlen('ic_control_data = ') );
                        return $new_str; //返回JSON对象
                } else {
                    return array();
                }
                                    
        }
}
$msn = new MSN;
$res = $msn->getAddressList('username@111.com','123456');
echo $res;
?>
<mce:script type="text/javascript"><!--
var data = <?php echo $res; ?>
alert(data['ic1'][6]);
alert(data['ic1'][3]);
// --></mce:script>

3.YAHOO
<?php
define( "COOKIEJAR", tempnam( ini_get( "upload_tmp_dir" ), "cookie" ) );   //定义COOKIES存放的路径,要有操作的权限
define( "TIMEOUT", 1000 ); //超时设定
class YAHOO
{
        private function login($username, $password)
        {               
                //第一步:模拟抓取登录页面的数据,并记下cookies
                $cookies = array();
                $matches = array();                //获取表单
                $login_url = "https://login.yahoo.com/config/login?.src=fpctx&.intl=us&.done=http%3A%2F%2Fwww.yahoo.com%2F";
                $ch = curl_init($login_url);
                curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
                curl_setopt($ch, CURLOPT_COOKIEJAR, COOKIEJAR);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                $contents = curl_exec($ch);
                curl_close($ch);
                //构造参数
                $name = array('tries','src','md5','hash','js','last','promo','intl','bypass','partner','u','v','challenge','yplus','emailCode','pkg','stepid','ev','hasMsgr','chkP','done','pd','pad','aad'); 
                $postfiles = array();
                $matches = array();
                foreach($name as $v) {
                     preg_match('/<input/s*type="hidden"/s*name=".'.$v.'"/s*value="(.*?)"/s*>/i', $contents, $matches);
                    if(!empty($matches)) {
                        $postfiles['.'.$v] = $matches[1];
                        $matches = array(); 
                    } 
                    if($v == 'pd') {
                        $postfiles['.'.$v] = urlencode($postfiles['.'.$v]);
                    }       
                }
                $postfiles['pad'] = 5; 
                $postfiles['aad'] = 6;
                $postfiles['login'] = urlencode($username);
                $postfiles['passwd'] = $password;
                $postfiles['.persistent'] = 'y'; 
                $postfiles['save'] = ''; 
                $postfiles['.done'] = urlencode($postfiles['.done']);
                //$postfiles['.pd'] = urlencode($postfiles['.pd']); 
                $postargs = '';
                foreach($postfiles as $k => $v){
                    $postargs .= $k.'='.$v.'&';        
                }
                $postargs = substr($postargs,0,-1);
                $request = "https://login.yahoo.com/config/login?";
                //开始登录
                $ch = curl_init();
                curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
                curl_setopt($ch, CURLOPT_URL, $request);
                curl_setopt($ch, CURLOPT_POST, 1);
                curl_setopt($ch, CURLOPT_POSTFIELDS, $postargs);
                curl_setopt($ch, CURLOPT_COOKIEJAR, COOKIEJAR);
                curl_setopt($ch, CURLOPT_TIMEOUT, TIMEOUT);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                $contents = curl_exec($ch);
                curl_close($ch); 
                if (stripos($contents,'submit') != FALSE)
                {                        
                        return 0;
                }        
                return 1;
        }
        //获取邮箱通讯录-地址
        public function getAddressList($username, $password)
        {               
                if (!$this->login($username, $password))
                {
                        return 0;
                }
            //开始进入模拟抓取
            //get mail list from the page information username && emailaddress
            $url = "http://address.mail.yahoo.com/";
            $data = array( );
            if ( !$data = $this->hanlde_date( $url, $names, $emails) )
            {
                return FALSE;
            }
            echo '<pre>';
            print_r($data);
            return $data;
        }
        function hanlde_date( $url, &$names, &$emails)
        {
            $ch = curl_init( );
            curl_setopt( $ch, CURLOPT_COOKIEFILE, COOKIEJAR );
            curl_setopt( $ch, CURLOPT_URL, $url );
            curl_setopt( $ch, CURLOPT_TIMEOUT, TIMEOUT );
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            $contents = curl_exec($ch);
            curl_close($ch); 
            $temparr = array();
            preg_match_all('/InitialContacts/s*=/s*(.*?);/i',$contents,$temparr);
            return $temparr[1][0].';';       //匹配出JSON对象数组
        }
}
$yahoo = new YAHOO;
$res = $yahoo->getAddressList('username@yahoo.com.cn','123456');
?>
<mce:script type="text/javascript"><!--
var data = <?php echo $res; ?>
var obj1 = data[0];
alert(data[0]['contactName']);
// --></mce:script>

注:
163,126在网上的源码都有,在此就不一一的列出来了哦
当然此代码是自己个人的拙作,只是为了给大家此类问题做一个指引
PHP 相关文章推荐
apache rewrite_module模块使用教程
Jan 10 PHP
php 方便水印和缩略图的图形类
May 21 PHP
php中使用临时表查询数据的一个例子
Feb 03 PHP
PHP实现批量生成App各种尺寸Logo
Mar 19 PHP
php简单获取文件扩展名的方法
Mar 24 PHP
详解 PHP加密解密字符串函数附源码下载
Dec 18 PHP
Yii框架上传图片用法总结
Mar 28 PHP
yii2.0数据库迁移教程【多个数据库同时同步数据】
Oct 08 PHP
PHP使用两个栈实现队列功能的方法
Jan 15 PHP
PHP一致性hash分布式算法封装类定义与用法示例
Aug 04 PHP
PHP封装的简单连接MongoDB类示例
Feb 13 PHP
apache集成php7.3.5的详细步骤
Jun 20 PHP
解析php中memcache的应用
Jun 18 #PHP
PHP实现的封装验证码类详解
Jun 18 #PHP
php empty()与isset()区别的详细介绍
Jun 17 #PHP
php include和require的区别深入解析
Jun 17 #PHP
浅析php header 跳转
Jun 17 #PHP
解析php中heredoc的使用方法
Jun 17 #PHP
深入PHP5中的魔术方法详解
Jun 17 #PHP
You might like
PHP目录函数实现创建、读取目录教程实例
2011/01/13 PHP
PHP 事件机制(2)
2011/03/23 PHP
PHP错误机制知识汇总
2016/03/24 PHP
PHP框架Laravel中实现supervisor执行异步进程的方法
2017/06/07 PHP
做网页的一些技巧
2007/02/01 Javascript
Javascript面向对象扩展库代码分享
2012/03/27 Javascript
js 获取后台的字段 改变 checkbox的被选中的状态 代码
2013/06/05 Javascript
jquery ajax 简单范例(界面+后台)
2013/11/19 Javascript
判断JS对象是否拥有某种属性的两种方式
2013/12/02 Javascript
使用原生js实现页面蒙灰(mask)效果示例代码
2014/06/20 Javascript
AngularJs解决跨域问题案例详解(简单方法)
2016/05/19 Javascript
任意Json转成无序列表的方法示例
2016/12/09 Javascript
如何给ss bash 写一个 WEB 端查看流量的页面
2017/03/23 Javascript
JavaScript实现的可变动态数字键盘控件方式实例代码
2017/07/15 Javascript
jQuery列表检索功能实现代码
2017/07/17 jQuery
微信小程序如何修改本地缓存key中单个数据的详解
2019/04/26 Javascript
小程序使用watch监听数据变化的方法详解
2019/09/20 Javascript
NodeJS有难度的面试题(能答对几个)
2019/10/09 NodeJs
vue.js自定义组件实现v-model双向数据绑定的示例代码
2020/01/08 Javascript
微信小程序纯文本实现@功能
2020/04/08 Javascript
Javascript幻灯片播放功能实现过程解析
2020/05/07 Javascript
webpack安装配置与常见使用过程详解(结合vue)
2020/06/01 Javascript
[02:36]DOTA2亚洲邀请赛小组赛精彩集锦:奇迹哥卡尔秀翻全场
2017/03/28 DOTA
[02:43]2018DOTA2亚洲邀请赛主赛事首日TOP5
2018/04/04 DOTA
python3爬虫学习之数据存储txt的案例详解
2019/04/24 Python
Python实现字符串中某个字母的替代功能
2019/10/21 Python
Python原始套接字编程实例解析
2020/01/29 Python
Keras 在fit_generator训练方式中加入图像random_crop操作
2020/07/03 Python
Django 实现图片上传和下载功能
2020/12/31 Python
Python 实现一个简单的web服务器
2021/01/03 Python
世界第一曲奇连锁店:Mrs. Fields Cookies
2017/02/04 全球购物
Lulu Guinness露露·吉尼斯官网:红唇包
2019/02/03 全球购物
医学实习生自我鉴定
2013/12/12 职场文书
2014年元旦促销活动方案
2014/02/22 职场文书
Python实现简单得递归下降Parser
2022/05/02 Python
win10滚动条自动往上跑怎么办?win10滚动条自动往上跑的解决方法
2022/08/05 数码科技