Ошибка:
Warning: file_get_contents(https://oauth.vk.com/access_token?client_id=5993817&client_secret=CjHNffXCDqfVJ6PrK3pa&code=1e0853a00ad9cd3f7e&redirect_uri=http://localhost/): failed to open stream: HTTP request failed! HTTP/1.1 401 Unauthorized in C:\OpenServer\domains\localhost\index.php on line 28
Сам код:
<?php
$client_id = '5934317'; // ID приложения
$client_secret = 'CjHNffXCdfAVJ6PrK3pa'; // Защищённый ключ
$redirect_uri = 'http://localhost/'; // Адрес сайта
$msgtoclient = '';
$url = 'http://oauth.vk.com/authorize';
$params = array(
'client_id' => $client_id,
'redirect_uri' => $redirect_uri,
'response_type' => 'code'
);
$link = $url .'?'. urldecode(http_build_query($params));
if (isset($_GET['code'])) {} else { header('Location:'.$link);};
if (isset($_GET['code'])) {
$result = false;
$params = array(
'client_id' => $client_id,
'client_secret' => $client_secret,
'code' => $_GET['code'],
'redirect_uri' => $redirect_uri
);
$token = json_decode(file_get_contents('https://oauth.vk.com/access_token' . '?' . urldecode(http_build_query($params))), true);
if (isset($token['access_token'])) {
$params = array(
'uids' => $token['user_id'],
'fields' => 'uid,first_name,last_name,screen_name,sex,bdate,photo_big',
'access_token' => $token['access_token']
);
$userInfo = json_decode(file_get_contents('https://api.vk.com/method/users.get' . '?' . urldecode(http_build_query($params))), true);
if (isset($userInfo['response'][0]['uid'])) {
$userInfo = $userInfo['response'][0];
$result = true;
}
}
if ($result) {
$msgtoclient = $userInfo;
} else {$msgtoclient = 'sdsdsd';}
}
echo json_encode($msgtoclient) ;
?>