Y
Y
yazux2017-03-11 19:49:31
PHP
yazux, 2017-03-11 19:49:31

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;
    }

I get the following response:
[error_code] => 15
[error_msg] => Access denied: no access to call this method

The documentation states that only standalone applications have access to wall.post, and I registered it.
Tell me why VK does not give the right to access the method? Maybe I missed something.

Answer the question

In order to leave comments, you need to log in

4 answer(s)
S
Sergey Sokolov, 2017-03-11
@yazux

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.

Z
zorca, 2017-03-11
@zorca

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);
}

S
SimBioT19, 2017-03-11
@SimBioT19

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.

E
e-rentier, 2017-03-21
@e-rentier

How can I get a token from the server?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question