gpt4 book ai didi

php - 如何使用 PHP 从 Gmail 导入联系人

转载 作者:行者123 更新时间:2023-12-03 09:25:52 25 4
gpt4 key购买 nike

我想使用 PHP 访问用户联系人。

我在/var/www/oauth 目录中有两个名为 index.php 和 validate.php 的文件。重定向到 validate.php 后,它现在显示任何内容。它正在打印“在curl init之前”的echo,但不打印“在curl init之后”的echo。请有人能帮助我。

Google 帐户设置:-

Client ID:  *********
Email address: *****
Client secret: ******
Redirect URIs: http://localhost/oauth/validate.php
JavaScript origins: http://localhost

这是我的 PHP 代码。

/var/www/oauth/index.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Login with gmail</title>
<style type="text/css">
body{background:url(random_grey_variations.png) repeat; }
#wrapper{width:960px; margin:0 auto; margin-top:100px;}
.button{display:block; height:40px; padding:0 20px; width:200px; background:#CCC; text-decoration:none; font:bold 18px Verdana, Geneva, sans-serif; color:#333; line-height:40px; text-align:center; margin-bottom:50px;}
</style>
</head>

<body>
<div id="wrapper">
<!-- <a href="http://webdevelopergeeks.com/tutorial/import-gmail-or-google-contacts-using-php-and-oauth-2-0/" class="button">Back To Article</a> -->
<a href="https://accounts.google.com/o/oauth2/auth?client_id=1015115465486.apps.googleusercontent.com&redirect_uri=http://localhost/oauth/validate.php&scope=https://www.google.com/m8/feeds/&response_type=code" class="button">Login Using Google</a>
</div>
</body>
</html>

/var/www/oauth/validate.php

<html>
<head><meta name="robots" content="noindex" /></head>
<body style="font-family: tahoma; font-size: 12px;">
<a href="http://webdevelopergeeks.com/tutorial/import-gmail-or-google-contacts-using-php-and-oauth-2-0/">Visit Article</a><hr>
<?php
//setting parameters
$authcode= $_GET["code"];
$clientid='1015115465486.apps.googleusercontent.com'; // client id
$clientsecret='----'; //Secret id
$redirecturi='http://localhost/oauth/validate.php'; // redirect uri [path to your validate.php]
// echo $authcode;
$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
**echo "before curl init";**
$ch = curl_init();
//set the url, number of POST vars, POST data
**echo "After curl init";**

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
echo $accesstoken;
$xmlresponse= file_get_contents('https://www.google.com/m8/feeds/contacts/default/full?oauth_token='.$accesstoken);
//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) {
echo $title->attributes()->address . "<br><br>";
}
?>
</body></html>

最佳答案

请检查 php_curl 扩展是否已启用,以便 cURL 功能适合您。

如果它已经启用,请发布详细的错误消息,以便我可以在最后检查。

关于php - 如何使用 PHP 从 Gmail 导入联系人,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21548687/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com