This repository has been archived by the owner on Apr 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathvk_api.php
99 lines (81 loc) · 2.47 KB
/
vk_api.php
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<?php
define('VK_API_VERSION', '5.67'); //Используемая версия API
define('VK_API_ENDPOINT', 'https://api.vk.com/method/');
function vkApi_messagesSend($peer_id, $message, $attachments = array()) {
return _vkApi_call('messages.send', array(
'peer_id' => $peer_id,
'message' => $message,
'attachment' => implode(',', $attachments)
));
}
function vkApi_usersGet($user_id) {
return _vkApi_call('users.get', array(
'user_id' => $user_id,
));
}
function vkApi_photosGetMessagesUploadServer($peer_id) {
return _vkApi_call('photos.getMessagesUploadServer', array(
'peer_id' => $peer_id,
));
}
function vkApi_photosSaveMessagesPhoto($photo, $server, $hash) {
return _vkApi_call('photos.saveMessagesPhoto', array(
'photo' => $photo,
'server' => $server,
'hash' => $hash,
));
}
function vkApi_docsGetMessagesUploadServer($peer_id, $type) {
return _vkApi_call('docs.getMessagesUploadServer', array(
'peer_id' => $peer_id,
'type' => $type,
));
}
function vkApi_docsSave($file, $title) {
return _vkApi_call('docs.save', array(
'file' => $file,
'title' => $title,
));
}
function _vkApi_call($method, $params = array()) {
$params['access_token'] = VK_API_ACCESS_TOKEN;
$params['v'] = VK_API_VERSION;
$query = http_build_query($params);
$url = VK_API_ENDPOINT.$method.'?'.$query;
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$json = curl_exec($curl);
$error = curl_error($curl);
if ($error) {
log_error($error);
throw new Exception("Failed {$method} request");
}
curl_close($curl);
$response = json_decode($json, true);
if (!$response || !isset($response['response'])) {
log_error($json);
throw new Exception("Invalid response for {$method} request");
}
return $response['response'];
}
function vkApi_upload($url, $file_name) {
if (!file_exists($file_name)) {
throw new Exception('File not found: '.$file_name);
}
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, array('file' => new CURLfile($file_name)));
$json = curl_exec($curl);
$error = curl_error($curl);
if ($error) {
log_error($error);
throw new Exception("Failed {$url} request");
}
curl_close($curl);
$response = json_decode($json, true);
if (!$response) {
throw new Exception("Invalid response for {$url} request");
}
return $response;
}