如何使用 PHP 从 Google 获取完整的联系信息?

编程入门 行业动态 更新时间:2024-10-28 11:29:55
本文介绍了如何使用 PHP 从 Google 获取完整的联系信息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我使用 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/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/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/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/m8/feeds/contacts/default/full

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

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

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

更多推荐

[db:关键词]

本文发布于:2023-04-18 09:31:20,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/936322.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   完整   信息   Google   PHP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!