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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
| <?php
class VKPublic
{
protected $groupId, $appId, $secretKey, $accessToken, $accessSecret;
/**
* @param int $groupId
* @param int $appId
* @param string $secretKey
*/
public function __construct($groupId, $appId, $secretKey)
{
$this->groupId = $groupId;
$this->appId = $appId;
$this->secretKey = $secretKey;
}
/**
* @param string $accessToken
* @param string $accessSecret
*/
public function setAccessData($accessToken, $accessSecret)
{
$this->accessToken = $accessToken;
$this->accessSecret = $accessSecret;
}
/**
* Hack
*/
public function getAccessData()
{
echo "<!doctype html><html><head><meta charset='utf-8'></head>
<body><a href='https://oauth.vk.com/authorize?" .
"client_id={$this->appId}&scope=offline,wall,groups,pages," .
"photos,docs,audio,video,notes,stats,messages,notify,notifications,nohttps&" .
"redirect_uri=https://oauth.vk.com/blank.html&response_type=code'
target='_blank'>Получить CODE</a><br>Ссылка для получения токена:<br>
<b>https://oauth.vk.com/access_token?client_id={$this->appId}" .
"&client_secret={$this->secretKey}&code=CODE</b></body></html>";
exit;
}
/**
* @param string $method
* @param mixed $parameters
* @return mixed
*/
public function callMethod($method, $parameters)
{
if (!$this->accessToken) return false;
if (is_array($parameters)) $parameters = http_build_query($parameters);
$queryString = "/method/$method?$parameters&access_token={$this->accessToken}";
$querySig = md5($queryString . $this->accessSecret);
return json_decode(file_get_contents(
"http://api.vk.com{$queryString}&sig=$querySig"
));
}
/**
* @param string $message
* @param bool $fromGroup
* @param bool $signed
* @return mixed
*/
public function wallPostMsg($message, $fromGroup = true, $signed = false)
{
return $this->callMethod('wall.post', array(
'owner_id' => -1 * $this->groupId,
'message' => $message,
'from_group' => $fromGroup ? 1 : 0,
'signed' => $signed ? 1 : 0,
));
}
/**
* @param string $attachment
* @param null|string $message
* @param bool $fromGroup
* @param bool $signed
* @return mixed
*/
public function wallPostAttachment($attachment, $message = null, $fromGroup = true, $signed = false)
{
return $this->callMethod('wall.post', array(
'owner_id' => -1 * $this->groupId,
'attachment' => strval($attachment),
'message' => $message,
'from_group' => $fromGroup ? 1 : 0,
'signed' => $signed ? 1 : 0,
));
}
/**
* @param string $file relative file path
* @return mixed
*/
public function createPhotoAttachment($file)
{
$result = $this->callMethod('photos.getWallUploadServer', array(
'gid' => $this->groupId
));
$ch = curl_init($result->response->upload_url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, array(
'photo' => '@' . getcwd() . '/' . $file
));
if (($upload = curl_exec($ch)) === false) {
throw new Exception(curl_error($ch));
}
curl_close($ch);
$upload = json_decode($upload);
$result = $this->callMethod('photos.saveWallPhoto', array(
'server' => $upload->server,
'photo' => $upload->photo,
'hash' => $upload->hash,
'gid' => $this->groupId,
));
return $result->response[0]->id;
}
public function combineAttachments()
{
$result = '';
if (func_num_args() == 0) return '';
foreach (func_get_args() as $arg) {
$result .= strval($arg) . ',';
}
return substr($result, 0, strlen($result) - 1);
}
}
?> |