`
modun
  • 浏览: 147043 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

使用curl获取google联系人列表 (向zend的Gdata say no)

阅读更多

登陆google

 

public function googleLogin($email,$password){
  		$session = UserOper::openSession();//如果已经登陆,直接返回
  		if($session['googleAuth']){
  			$session->close();
  			return true;
  		}
  		$data = array(  
		    'accountType' => 'GOOGLE',  
		    'Email' => $email,  
		    'Passwd' => $password,  
		    'service' => 'cp',  //google 一系列api 的简写,在google 上能找到,可以换成你想要的服务简写
		    'source' => 'test-oauth-1.0',  //给你自己的应用程序命名
		);  
		$ch = curl_init();  
		curl_setopt($ch, CURLOPT_URL, "https://www.google.com/accounts/ClientLogin");  
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  
		curl_setopt($ch, CURLOPT_POST, true);  
		curl_setopt($ch, CURLOPT_POSTFIELDS, $data);  
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); 
		$output = curl_exec($ch);
		$info = curl_getinfo($ch); 
		preg_match('/Auth=.+/',$output,$tempArray);
		if($info['http_code']!=200 or empty($tempArray)){
			return false;
		}
		$auth = 'Authorization: GoogleLogin auth='.substr($tempArray[0],5); 
		$session['googleAuth'] = $auth;
		return true;
  	}

 获取联系人信息(atom格式数据源)

 

public function getGoogleResource($url){
  		$session = UserOper::openSession();
  		if(!$session['googleAuth']){
  			$session->close();
  			return false;
  		}
  		$session->close();
  		$ch = curl_init();  
		curl_setopt($ch, CURLOPT_URL,$url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
		curl_setopt($ch, CURLOPT_HTTPHEADER,array($session['googleAuth']));  
		$output = curl_exec($ch);
		$info = curl_getinfo($ch);
		if($info['http_code']!=200) return false;
		return $output;
  	}

 解析数据源,读取联系人email地址(使用 php DOMDocument)

 

public function getGoogleFriends(){
  		$url = 'http://www.google.com/m8/feeds/contacts/default/full';
  		$source = $this->getGoogleResource($url);
  		$friends = array();
  		if($source){
  			$dom = new DOMDocument();
  			$dom->loadXML($source);
  			$entries = $dom->getElementsByTagName('entry');
  			foreach ( $entries as $entry ){
  				$email = $entry->getElementsByTagName('email');
  				$value = $email->item(0)->getAttributeNode("address")->value;
  				$friends[$value] = $value;
  			}
  			return $friends;
  		}
  		return false;
  	}
分享到:
评论
1 楼 qesc 2010-05-04  
想知道,楼主调试CURL模拟登陆邮件的时候,用了哪些方便的软件吗?上次写CURL模拟登陆SOHU抓邮箱好友,只会用FF下的FireBug,感觉挺麻烦的。

相关推荐

Global site tag (gtag.js) - Google Analytics