使用php / curl登录谷歌并保持登录状态

时间:2012-11-05 11:36:24

标签: php curl google-login

我尝试使用php curl登录谷歌。我找到了这篇文章:

Login to Google with PHP and Curl, Cookie turned off?

效果很好。但我的目标是在发布登录请求后保持登录状态。我想维护浏览器会话,所以当我在新标签中打开gmail或任何谷歌服务时,它应该保持登录状态。

有可能吗?如有,任何帮助或建议都将受到高度赞赏。

如果我的问题不够明确,请告诉我。

谢谢!

1 个答案:

答案 0 :(得分:0)

<?php

/* Google App Client Id */
define('CLIENT_ID', 'xxxxxxxxxxxxxxxxx');

/* Google App Client Secret */
define('CLIENT_SECRET', 'xxxxxxxxxxxxxxx');

/* Google App Redirect Url */
define('CLIENT_REDIRECT_URL', 'xxxxxxxxxxxxxxx');

$login_url = 'https://accounts.google.com/o/oauth2/v2/auth?scope=' . urlencode('https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email') . '&redirect_uri=' . urlencode(CLIENT_REDIRECT_URL) . '&response_type=code&client_id=' . CLIENT_ID . '&access_type=online';
if(!$_GET['a'] == callback){
?>
<html>
<head>
  <link href="https://fonts.googleapis.com/css?family=Roboto" rel="stylesheet" type="text/css">
  <style type="text/css">
    #customBtn {
      display: inline-block;
      background: white;
      color: #444;
      width: 190px;
      border-radius: 5px;
      border: thin solid #888;
      box-shadow: 1px 1px 1px grey;
      white-space: nowrap;
    }
    #customBtn:hover {
      cursor: pointer;
    }
    span.label {
      font-family: serif;
      font-weight: normal;
    }
    span.icon {
      background: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAIAAAD8GO2jAAAB2ElEQVR4AWP4T2NAJwtGLRi14NeNq5+6m99lxb8OcH7lbfs2LuhTd9PP08epYMGfF8/fF2W8cjbFit7lp/x59oR8C35dvfQ62B1oEB70vjSbTAv+vH752t8J2ay38cEfmyo/NJS/iQ2E+iAj9u+nj2Ra8L40E270mwjvH8cOIcv+OLTvQ1UBxHRyLPj37vCXXvFXbsYg08M8/zx9QuVU9Oda+u89rN+X8r0J1v++cwv1k+nv44ZAC4Do5w6Jf3/+IEu1bfyBFR26/psUC/YLQyz4fdoeTcqp9QtWNGnHD9pa0LOFJAuOG0As+HxQ7s9flCCKm/4VjiKnfIVbMGf/TxIs+HklBWj61R3Cfqs8ttzbjysC15/+Bbdg1yVS4uD7673rtyhZrQgwWRHssSHl8ecXmGo+fP0XMw3qA+fWL0AuaRkte38T0HQI8t6YdujpaWTZG+/uJcy7B3d+28bvJOfkl1/fOK6NA5oOR0FbciqO9lYf64/ZWQrkmi6Ltu0/CDTdt+fLiw9/ySnsLr2+6bY+CWgWHuS9cPG5+3/IL66ff3mVvq8Ol+khW/Ouv7tLhQrn6tvbTSenxu0qd1oXb7060m9zZtWx/j2Pjv3993c41MmjFoxaAAB8n/tkbTPVXwAAAABJRU5ErkJggg==') transparent 5px 50% no-repeat;
      display: inline-block;
      vertical-align: middle;
      width: 42px;
      height: 42px;
    }
    span.buttonText {
      display: inline-block;
      vertical-align: middle;
      padding-left: 42px;
      padding-right: 42px;
      font-size: 14px;
      font-weight: bold;
      /* Use the Roboto font that is loaded in the <head> */
      font-family: 'Roboto', sans-serif;
    }
  </style>
</head>

<body>
  <div id="gSignInWrapper">
    <a href="<?= $login_url ?>"><div id="customBtn" class="customGPlusSignIn">
      <span class="icon"></span>
      <span class="buttonText">Google</span>
    </div></a>
  </div>
</body>
<?php
} else {
// Holds the various APIs functions
function GetAccessToken($client_id, $redirect_uri, $client_secret, $code) { 
    $url = 'https://www.googleapis.com/oauth2/v4/token';            

    $curlPost = 'client_id=' . $client_id . '&redirect_uri=' . $redirect_uri . '&client_secret=' . $client_secret . '&code='. $code . '&grant_type=authorization_code';
    $ch = curl_init();      
    curl_setopt($ch, CURLOPT_URL, $url);        
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);        
    curl_setopt($ch, CURLOPT_POST, 1);      
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);    
    $data = json_decode(curl_exec($ch), true);
    $http_code = curl_getinfo($ch,CURLINFO_HTTP_CODE);      
    if($http_code != 200) 
        throw new Exception('Error : Failed to receieve access token');
    
    return $data;
}

function GetUserProfileInfo($access_token) {    
    $url = 'https://www.googleapis.com/oauth2/v2/userinfo?fields=name,email,gender,id,picture,verified_email';  
    
    $ch = curl_init();      
    curl_setopt($ch, CURLOPT_URL, $url);        
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Bearer '. $access_token));
    $data = json_decode(curl_exec($ch), true);
    $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);     
    if($http_code != 200) 
        throw new Exception('Error : Failed to get user information');
        
    return $data;
}

// Google passes a parameter 'code' in the Redirect Url
if(isset($_GET['code'])) {
    try {
        // Get the access token 
        $data = GetAccessToken(CLIENT_ID, CLIENT_REDIRECT_URL, CLIENT_SECRET, $_GET['code']);

        // Access Token
        $access_token = $data['access_token'];
        
        // Get user information
        $user_info = GetUserProfileInfo($access_token);
        
        var_dump($user_info);

        echo $user_info['id'] . "<br>";

        echo $user_info['email'] . "<br>";
        
        echo $user_info['name'] . "<br>";
        
        echo "<img src=".$user_info['picture'].">";
        


    }
    catch(Exception $e) {
        echo $e->getMessage();
        exit();
    }
}
}
?>