Answer the question
In order to leave comments, you need to log in
How to make a post on the group wall using the Vkontakte wall.post API?
I registered a standalone application on VKontakte, received a token using the url:
https://oauth.vk.com/authorize?client_id=[my application id here]&scope=manage,groups,wall,offline&redirect_uri= oauth.vk.com /blank.html&response_type =token
Making a request for a community wall post:
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
Answer the question
In order to leave comments, you need to log in
The token must be obtained from the same IP as the method will be called. You will call from the server - you need to receive a token using this server as a proxy for the browser to enter VK.
Working option:
<?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);
}
Nothing will come of it. You cannot call the wall.post method from the server . VK is checking ip.
If the ip to which the token was issued does not match the ip from which the method is called, get an error.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question