下面的代码不是打印userinfo,而是打印Google授予的访问令牌的详细信息。代码很简单,但我无法找出的确切原因
<?php
$url = 'https://accounts.google.com/o/oauth2/token';
$fields = array(
'code'=>urlencode($_GET['code']),
'client_id'=>urlencode('###########.apps.googleusercontent.com'),
'client_secret'=>urlencode('###########'),
'redirect_uri'=>urlencode('http://######odie.co.in/googlogin'),
'grant_type'=>urlencode('authorization_code'),
);
foreach($fields as $key=>$value) {
$fields_string .= $key.'='.$value.'&';
}
rtrim($fields_string,'&');
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);
$data = json_decode($result);
print_r(file_get_contents(' https://www.googleapis.com/oauth2/v1/userinfo?access_token='.$data['access_token']));
?>
您需要将CURLOPT_RETURNTRANSFER, true
添加到curl_setopt中。
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
请参阅此。它非常简单。
用于Google身份验证的简单PHP库API