在PHP中使用Google身份验证API

在PHP中使用Google身份验证API,php,google-api,Php,Google Api,下面的代码不是打印userinfo,而是打印Google授予的访问令牌的详细信息。代码非常简单,但我无法找出造成这种情况的确切原因 <?php $url = 'https://accounts.google.com/o/oauth2/token'; $fields = array( 'code'=>urlencode($_GET['code']), 'client_id'=>urlencode('###########.apps.goog

下面的代码不是打印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\u RETURNTRANSFER,true
添加到curl\u setopt

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

参考这个。这很简单