M
M
Millerish2018-03-09 11:32:17
PHP
Millerish, 2018-03-09 11:32:17

VK Communities: how to send a message with a photo?

Good time of the day!
I need to send a message on behalf of the community with a photo attachment. To do this, you must first upload a photo, such as this request:

$token = 'XXX';
$group_id = '162294758';
$album_id = '162294758';
$v = '5.64'; //версия vk api
$image_path = "http://worldwideshop.ru/vbot-dev/api/test.jpg"; //путь до картинки
$fp = fopen($image_path, 'r');

// получаем урл для загрузки
$url = file_get_contents("https://api.vk.com/method/photos.getUploadServer?album_id=".$album_id."&group_id=".$group_id."&v=".$v."&access_token=".$token);
$url = json_decode($url)->response->upload_url;
print_r($url);

// отправка post картинки
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_INFILE, $fp);
$result = json_decode(curl_exec($ch),true);
print_r($result);

// сохраняем
$safe = file_get_contents("https://api.vk.com/method/photos.save?server=".$result['server']."&photos_list=".$result['photos_list']."&album_id=".$result['aid']."&hash=".$result['hash']."&gid=".$group_id."&access_token=".$token);
$safe = json_decode($safe,true);
print_r($safe);

Array ( [error] => Array ( [error_code] => 5 [error_msg] => User authorization failed: method is unavailable with group auth. [request_params] => Array ( [0] => Array ( [key] => oauth [value] => 1 ) [1] => Array ( [key] => method [value] => photos.save ) [2] => Array ( [key] => server [value] => ) [ 3] => Array ( [key] => photos_list [value] => ) [4] => Array ( [key] => album_id [value] => ) [5] => Array ( [key] => hash [value] => ) [6] => Array ( [key] => gid [value] => 162294758 ) ) ) )

Questions:
  1. Where is the right place to upload this photo initially? If in a community album, should it be public?
  2. Tell me a simple example for downloading an attachment (on the net or ready-made large solutions or old non-working pieces of code, such as links to git here ).

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
VK API, 2018-03-09
@vkapi

0. You can’t post your token in the public domain like this! We hope he is already disabled.
1. To upload photos to the dialog, you need to use special methods:
https://vk.com/dev/photos.getMessagesUploadServer
https://vk.com/dev/photos.saveMessagesPhoto
2. The version parameter v is missing in the second request, and it is required in any request.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question