来自php的Gmail帐户身份验证

时间:2015-04-09 06:29:31

标签: php gmail

我正在尝试使用this教程验证Google帐户。我只传递我的用户名和密码,但它返回false。我是新手。这是我打电话的功能

function googleAuthenticate($username, $password, $source = 'your_google_app', $service = 'lh2') {
$session_token = $source . '_' . $service . '_auth_token';

// get an authorization token
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://www.google.com/accounts/ClientLogin");
$post_fields = "accountType=" . urlencode('GOOGLE')
    . "&Email=" . urlencode($username)
    . "&Passwd=" . urlencode($password)
    . "&source=" . urlencode($source)
    . "&service=" . urlencode($service);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, TRUE);
curl_setopt($ch, CURLINFO_HEADER_OUT, true); // for debugging the request
curl_setopt($ch, CURLOPT_VERBOSE, true); // for debugging the request
var_dump(curl_getinfo($ch,CURLINFO_HEADER_OUT));
echo "<br>";
var_dump($post_fields);
echo "<br>";

$response = curl_exec($ch);
curl_close($ch);

var_dump($response);
exit();

if (strpos($response, '200 OK') === false) {
    return false;
}

// find the auth code
preg_match("/(Auth=)([\w|-]+)/", $response, $matches);

if (!$matches[2]) {
    return false;
}

$_SESSION[$session_token] = $matches[2];
return $matches[2];

}

0 个答案:

没有答案