Answer the question
In order to leave comments, you need to log in
The picture is not attached to the post in the group, while the same code attaches the picture to the post on the user's wall. What am I doing wrong?
The picture is not attached to the post in the group, while the same code attaches the picture to the post on the user's wall. What am I doing wrong?
I also tried to use owner_id instead of $nam (in some examples they use it, although there is no contact api in the docks) in the group does not work for the user - it works.
$group_id='-153622138'; //id сообщества
// $group_id='10085174';//id юзера
// почему то без этой хрени не работает, сокорее всего из-за версии php. Этот костыль нашел, когда ругалось на curl_file_create
if (!function_exists('curl_file_create')) {
function curl_file_create($filename, $mimetype = '', $postname = '') {
return "@$filename;filename="
. ($postname ?: basename($filename))
. ($mimetype ? ";type=$mimetype" : '');
}
}
$text = 'ТЕСТ';
$image_name = 'test.jpg';
$image = __DIR__ . DIRECTORY_SEPARATOR . $image_name;
if ($group_id<0) {
$nam='group_id';
} else {
$nam='user_id';
}
$val=abs($group_id);
$upload_url = vkAPI('photos.getWallUploadServer', [$nam => $val])->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('Неизвестная ошибка при попытке загрузки картинки '.$e);
}
$responseSave = vkAPI('photos.saveWallPhoto', [
$nam => $val,
'photo' => stripslashes($responseUpload->photo),
'server' => $responseUpload->server,
'hash' => $responseUpload->hash,
]);
if ($responseSave->error) {
var_dump($responseSave);
exit('Неизвестная ошибка при попытке сохранения картинки ');
} else {
echo 'Картинка успешно сохранена<br>';
}
echo 'photo'.$group_id.'_'.$responseSave->response[0]->id;
$responsePost = vkAPI('wall.post', [
'owner_id' => $group_id,
'message' => $text,
'attachments' => 'photo'.$group_id.'_'.$responseSave->response[0]->id,
'hash' => $responseSave->response[0]->hash,
]);
if ($responsePost->error) {
if ($responsePost->error->error_code == 214) {
exit('Стена закрыта для постинга');
} else {
var_dump($responsePost->error);
exit('Неизвестная ошибка при попытке постинга');
}
} else {
echo 'Пост успешно добавлен';
}
function vkAPI($method, array $data = [])
{
global $config;
$params = [];
foreach ($data as $name => $val) {
$params[$name] = $val;
$params['access_token'] = $config->vk_token;
$params['v'] = $config->vk_version;
}
$json = file_get_contents('https://api.vk.com/method/' . $method . '?' . http_build_query($params));
return json_decode($json);
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question