如何使用PHP从谷歌获取完整的联系方式? [英] How to get full contact information from Google using PHP?

查看:220
本文介绍了如何使用PHP从谷歌获取完整的联系方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用OAuth 2.0导入联系人,但我只获得的电子邮件地址。什么方法可以把其他领域?另外,我如何创建使用谷歌API的联系人。
只需要使用PHP。

下面是我的code:

  //设置参数
$ AUTH code = $ _GET [code];
$的clientid ='XXXXXXX';
$ clientsecret ='秘密';
$ redirecturi ='validate.php';
$栏=阵列(
 code'=> urlen code($ AUTH code)
 CLIENT_ID'=> urlen code($的clientid)
 client_secret'=> urlen code($ clientsecret)
 REDIRECT_URI'=> urlen code($ redirecturi)
 grant_type'=> urlen code('authorization_ code')
);
// URL-IFY数据为POST
$ fields_string ='';
的foreach($领域$关键=> $值){$ fields_string = $键'='$价值'和;'。; }
$ fields_string = RTRIM($ fields_string,'和;');
//打开连接
$ CH = curl_init();
//设置URL,POST数瓦尔,POST数据
curl_setopt($ CH,CURLOPT_URL,的https://accounts.google.com/o/oauth2/token');
curl_setopt($ CH,CURLOPT_POST,5);
curl_setopt($ CH,CURLOPT_POSTFIELDS,$ fields_string);
//设置这样curl_exec返回结果,而不是将其输出。
curl_setopt($ CH,CURLOPT_RETURNTRANSFER,真正的);
//信任任何SSL证书
curl_setopt($ CH,CURLOPT_SSL_VERIFYPEER,FALSE);
//实行岗位
$结果= curl_exec($ CH);
//关闭连接
curl_close($ CH);
//从响应字符串中提取的access_token
$响应= json_de code($结果);
$的accessToken = $响应 - >的access_token;
//路过的accessToken获取联系方式
$ xmlresponse =的file_get_contents('https://www.google.com/m8/feeds/contacts/default /full?oauth_token='.$accesstoken.'&max-results=5');
//使用SimpleXML读取XML
 $ XML =新的SimpleXMLElement($ xmlresponse);
 $ XML-> registerXPathNamespace('GD','http://schemas.google.com/g/2005');
 $结果= $ XML的>的XPath('// GD:电子邮件');
 的foreach($结果为$标题){
  $ addrss = $标题 - >属性() - >地址;
  回声$ addrss< BR>< BR>中。


解决方案

好吧,如果你只为解析的XML GD:电子邮件,那么你肯定只能得到电子邮件地址。请参阅联系人种类文档为元素的概述你也可以为解析

有关创建联系人,则可以只发出一份载有身体的接触细节同一端点的POST请求:

  https://www.google.com/m8/feeds/contacts/default/full

有关的联络资料格式的详细资料,请参见 API文档

I am using Oauth 2.0 to Import Contacts but I am getting only the email addresses. Any Way to Get Other Fields? Also, How can I create Contacts using Google API. Need to use Only PHP.

Here is my Code:

//setting parameters
$authcode= $_GET["code"];
$clientid='xxxxxxx';
$clientsecret='secret';
$redirecturi='validate.php';
$fields=array(
 'code'=>  urlencode($authcode),
 'client_id'=>  urlencode($clientid),
 'client_secret'=>  urlencode($clientsecret),
 'redirect_uri'=>  urlencode($redirecturi),
 'grant_type'=>  urlencode('authorization_code')
);
//url-ify the data for the POST
$fields_string='';
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
$fields_string=rtrim($fields_string,'&');
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL,'https://accounts.google.com/o/oauth2/token');
curl_setopt($ch,CURLOPT_POST,5);
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
// Set so curl_exec returns the result instead of outputting it.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//to trust any ssl certificates
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
//extracting access_token from response string
$response=  json_decode($result);
$accesstoken= $response->access_token;
//passing accesstoken to obtain contact details
$xmlresponse=  file_get_contents('https://www.google.com/m8/feeds/contacts/default     /full?oauth_token='.$accesstoken.'&max-results=5');
//reading xml using SimpleXML
 $xml=  new SimpleXMLElement($xmlresponse);
 $xml->registerXPathNamespace('gd', 'http://schemas.google.com/g/2005');
 $result = $xml->xpath('//gd:email');
 foreach ($result as $title) {
  $addrss=$title->attributes()->address;
  echo $addrss."<br><br>";

解决方案

Well, if you're only parsing the XML for gd:email then you certainly only get the email address. See the Contact kind documentation for an overview of the elements you could also parse for.

For creating contacts you can just issue a POST request with the contact details in the body to the same endpoint:

https://www.google.com/m8/feeds/contacts/default/full

For detailed documentation on the format of the contact details, see the API documentation.

这篇关于如何使用PHP从谷歌获取完整的联系方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆