Это простая система, где пользователям для того, чтобы понравиться товар, они должны войти в Google, чтобы идентифицировать их.Нажатая кнопка содержит значение product_id
, но я не могу понять, как получить ее после входа и перенаправить на другую страницу после входа. Обратите внимание, что действие формы перенаправляет пользователя на вход в Google.
<form method="POST" action="<?= 'https://accounts.google.com/o/oauth2/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' ?>">
<button name="upvote" type="submit" class="btn btn-info" value="<?php echo $product_id; ?>">Like</button>
</form>
Место назначения (URL-адрес перенаправления) после того, как пользователь вошел в систему: receive.php
Вот мой полный рабочий код;
1.settings.php ЭТО ПРОСТО НАСТРОИТЬ API
<?php
/* Google App Client Id */
define('CLIENT_ID', '/*My client ID*/');
/* Google App Client Secret */
define('CLIENT_SECRET', '/*My client Secret*/');
/* Google App Redirect Url */
define('CLIENT_REDIRECT_URL', 'https://www.example.com/receier.php');
?>
2.google-login-api.php Чтобы отправить мои ключи в GOOGLE и получить пользовательские данные после входа в систему
<?php
class GoogleLoginApi
{
public 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;
}
public 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;
}
}
?>
3.likeproduct.php ЭТО ТАМ, ГДЕ НАЖИМАЕТСЯ КНОПКА, НАПРАВЛЯЮЩАЯ РЕГИСТРАЦИЮ В Google, КНОПКА СОДЕРЖИТ ЗНАЧЕНИЕ ID ПРОДУКТА
<form method="POST" action="<?= 'https://accounts.google.com/o/oauth2/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' ?>">
<button name="upvote" type="submit" class="btn btn-info" value="<?php echo $product_id; ?>">Like</button>
</form>
4.receiver.php ЭТОГДЕ ПОЛУЧАЕТСЯ ИНФОРМАЦИЯ ПОЛЬЗОВАТЕЛЯ GOOGLE ACCOUNT (ПЕРЕНАПРАВЛЯЮЩИЙ URL) И ТАКЖЕ, КОГДА Я НАМЕРЕН, ЧТОБЫ ПОЛУЧИТЬ $product_id
, КОТОРЫЕ ПРОЙДЕНО В КАЧЕСТВЕ КНОПКИ ПОДОБНОЙ В likeproduct.php
<?php
require_once('settings.php');
require_once('google-login-api.php');
// Google passes a parameter 'code' in the Redirect Url
if(isset($_GET['code'])) {
try {
$gapi = new GoogleLoginApi();
// Get the access token
$data = $gapi->GetAccessToken(CLIENT_ID, CLIENT_REDIRECT_URL, CLIENT_SECRET, $_GET['code']);
// Get user information
$user_info = $gapi->GetUserProfileInfo($data['access_token']);
}
catch(Exception $e) {
echo $e->getMessage();
exit();
}
}
?>
Как я могу получить идентификатор продукта на receiver.php после отправки?