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

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

问题描述

我使用 Oauth 2.0 导入联系人,但我只获取电子邮件地址.有没有办法获取其他字段?另外,如何使用 Google API 创建联系人.只需要使用 PHP.

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.

这是我的代码:

//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>";

推荐答案

好吧,如果您只是为 gd:email 解析 XML,那么您当然只能获得电子邮件地址.请参阅联系人类型文档,了解元素概述你也可以解析.

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.

要创建联系人,您只需向同一端点发出带有正文中的联系人详细信息的 POST 请求:

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

有关详细联系信息格式的详细文档,请参阅API 文档.

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

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

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