php define VK_APP_ID define VK_APP_SECRET ch curl_init curl_setopt_arr

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<?php
define('VK_APP_ID', '');
define('VK_APP_SECRET', '');
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_USERAGENT => "Ololo",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => false
));
$vk_redirect_uri = 'http://zhumarin.ru/api_test/vk.php';
if (!isset($_GET['code'])) {
$oauth_url = url(
'https://oauth.vk.com/authorize',
array(
'client_id' => VK_APP_ID,
'scope' => 'notify friends photos audio video docs notes pages status offers questions groups notifications stats ads offline nohttps',
'display' => '',
'response_type' => 'code',
'redirect_uri' => $vk_redirect_uri
)
);
header("Location: ".$oauth_url);
} else {
$res = request($ch, 'https://oauth.vk.com/access_token', array(
'client_id' => VK_APP_ID,
'client_secret' => VK_APP_SECRET,
'grant_type' => 'authorization_code',
'code' => $_GET['code'],
'redirect_uri' => $vk_redirect_uri
));
$data = json_decode($res, true);
if (is_array($data) && isset($data['access_token'])) {
var_export($data);
} else {
echo 'Error: <pre>'.htmlspecialchars($res).'</pre>';
}
}
function url($path, $args, $html = false) {
$url = $path.'?'.http_build_query($args, '', '&');
return $html ? htmlspecialchars($url) : $url;
}
function request($ch, $url, $data = array()) {
curl_setopt($ch, CURLOPT_URL, $url);
if ($data) {
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_POST, true);
}
$data = curl_exec($ch);
curl_setopt($ch, CURLOPT_POST, false);
return $data;
}