· 7 years ago · Mar 04, 2018, 03:42 PM
1<?php
2
3/**
4 * Ðвтор Олег ИÑаев
5 * ВКонтакте: vk.com/id50416641
6 * Skype: pandcar97
7 */
8
9// Пример
10if ($array = vk_auth('http://oauth2.ru/test.php', '6395840', '7oTp5Zvkq2FVEhWzHUIh'))
11{
12 // Код авторизации/региÑтрации
13 echo '<pre>';
14 print_r($array);
15}
16
17
18function vk_auth($redirect_url, $app_id, $secret_key, $fields = 'id,first_name,last_name,sex,bdate,city,country,home_town,photo_50,photo_200_orig')
19{
20 $api_version = '5.27';
21
22 if (empty($_GET['error']))
23 {
24 if (empty($_GET['code']))
25 {
26 header('Location:https://oauth.vk.com/authorize?client_id='.$app_id.'&scope=&display=mobile&redirect_uri='.urlencode($redirect_url).'&response_type=code&v='.$api_version);
27 exit;
28 }
29 else
30 {
31 $access = json_decode(file_get_contents('https://oauth.vk.com/access_token?client_id='.$app_id.'&client_secret='.$secret_key.'&code='.$_GET['code'].'&redirect_uri='.urlencode($redirect_url)), true);
32
33 if (! empty($access['access_token']) && empty($access['error']))
34 {
35 $user = json_decode(file_get_contents('https://api.vk.com/method/users.get?user_ids='.$access['user_id'].'&fields='.$fields.'&v='.$api_version.'&access_token='.$access['access_token']), true);
36
37 if (empty($user['error']))
38 {
39 $user['response']['0']['access_token'] = $access['access_token'];
40
41 return $user['response']['0'];
42 }
43 }
44 }
45 }
46
47 return false;
48}