public function post(){
$result['request']['url'] = 'https://api.vk.com/method/wall.post';
$result['request']['params'] = array(
'owner_id' => '-'.$this->owner_id, // id сообщества
'access_token' => $this->token, // access_token
'friends_only' => 0,
'from_group' => 1,
'message' => 'test post'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $result['request']['url']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query( $result['request']['params'] ));
$response = curl_exec($ch);
$result['response'] = json_decode($response);
return $result;
}
[error_code] => 15
[error_msg] => Access denied: no access to call this method
<?php
CONST TOKEN = '?:%*?:%*?:%*?:%*?:%*';
$user_id = 666;
$text = 'Постим на страничку от имени Steelcat';
$image_name = 'steelcat.jpg';
$image = __DIR__ . DIRECTORY_SEPARATOR . $image_name;
$upload_url = vkAPI('photos.getWallUploadServer', ['owner_id' => $user_id])->response->upload_url;
try {
$ch = curl_init($upload_url);
$cfile = curl_file_create($image, mime_content_type($image), $image_name);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, ['photo' => $cfile]);
$responseUpload = json_decode(curl_exec($ch));
curl_close($ch);
echo 'Картинка успешно загружена<br>';
} catch (Exception $e) {
exit('Неизвестная ошибка при попытке загрузки картинки');
}
$responseSave = vkAPI('photos.saveWallPhoto', [
'owner_id' => $user_id,
'photo' => stripslashes($responseUpload->photo),
'server' => $responseUpload->server,
'hash' => $responseUpload->hash,
]);
if ($responseSave->error) {
exit('Неизвестная ошибка при попытке сохранения картинки');
} else {
echo 'Картинка успешно сохранена<br>';
}
$responsePost = vkAPI('wall.post', [
'owner_id' => $user_id,
'message' => $text,
'attachments' => $responseSave->response[0]->id,
'hash' => $responseSave->response[0]->hash,
]);
if ($responsePost->error) {
if ($responsePost->error->error_code == 214) {
exit('Стена закрыта для постинга');
} else {
exit('Неизвестная ошибка при попытке постинга');
}
} else {
echo 'Пост успешно добавлен';
}
function vkAPI($method, array $data = [])
{
$params = [];
foreach ($data as $name => $val) {
$params[$name] = $val;
$params['access_token'] = TOKEN;
}
$json = file_get_contents('https://api.vk.com/method/' . $method . '?' . http_build_query($params));
return json_decode($json);
}