日期:2014-05-17 浏览次数:20345 次
<?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($co