---
title: "GMAIL模拟登陆"
author: "lcffufly"
date: "2012-01-14T17:26:48Z"
tags: []
categories: ["编程语言"]
slug: "gmail"
draft: false
lastmod: "2012-01-14T17:26:48Z"
id: 1345400
url: "https://lcffufly.iteye.com/blog/1345400"
---

\[code="java"] /i', $contents, $matches); if(!empty($matches)) { $$v = $matches\[1]; $matches = array(); } } $server = 'mail'; preg\_match('//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-&gt;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 = "pab:searchContactsFNtrueuser:getSignaturespab:getAllGroups"; 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("/(.\*)&lt;\\/string&gt;/Umsi",$contents,$mails); preg\_match\_all("/(.\*)&lt;\\/string&gt;/Umsi",$contents,$names); $users = array(); foreach($names\[1] as $k=&gt;$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-&gt;getAddressList('username@163.com','123456'); echo $res; ?&gt; \[/code]