PHP模拟Gmail登陆并获取联系人列表

<?php
define( "COOKIEJAR", tempnam( ini_get( "upload_tmp_dir" ), "cookie" ) );   //定义COOKIES存放的路径,要有操作的权限 
#define("COOKIEJAR",tempnam('d:/tmpcookie/', "cookie" ));
define( "TIMEOUT", 1000 ); //超时设定 
class gmail_contacts
{

	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 true;
	}
	 
	//获取邮箱通讯录-地址 
	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_contacts;
$res = $gamil->getAddressList('test@gmail.com','******');
 print_r($res);
*/



?>

转载于:https://www.cnblogs.com/promic/articles/2029388.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值