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

最近一直在忙,赶在这个假期结束的时候,发表一下此文,为了是让这些源码开源出来
当然这些方法可能不可取,但大致应该是这种方向了吧,由于目前做的一个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=https://www.jb51.net/"1.0/"?><object><array name=https://www.jb51.net/"items/"><object><string name=https://www.jb51.net/"func/">pab:searchContacts</string><object name=https://www.jb51.net/"var/"><array name=https://www.jb51.net/"order/"><object><string name=https://www.jb51.net/"field/">FN</string><boolean name=https://www.jb51.net/"ignoreCase/">true</boolean></object></array></object></object><object><string name=https://www.jb51.net/"func/">user:getSignatures</string></object><object><string name=https://www.jb51.net/"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=https://www.jb51.net/"EMAILREF/">(.*)<//string>/Umsi",$contents,$mails);
                preg_match_all("/<string/s*name=https://www.jb51.net/"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)

复制代码 代码如下:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/8e826debede022d0d5cc60e1f934750a.html