Answer the question
In order to leave comments, you need to log in
What is wrong with the publication of posts on VKontakte?
Hello. There is a function for posting to a group. The script works, but through time. Either publishes posts, then does not publish, I can not understand why. Used on VPS. The number of posts is about 5 per day with an interval of more than 1 hour. Please tell me what to pay attention to.
function vkpost($message, $img) {
$group_id = ''; // id сообщества (без минуса)
$access_token = ''; // токен приложения
// Получение сервера vk для загрузки изображения.
$res = json_decode(file_get_contents(
'https://api.vk.com/method/photos.getWallUploadServer?group_id='
. $group_id . '&access_token=' . $access_token
));
if (!empty($res->response->upload_url)) {
// Отправка изображения на сервер.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $res->response->upload_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
if ($img)
{
//curl_setopt($ch, CURLOPT_POSTFIELDS, array('photo' => '@' . "1.jpg"));
// если у вас php 5.6 + удалите // с начала строки у строки ниже
curl_setopt($ch, CURLOPT_POSTFIELDS, array('photo' => new CurlFile($img))); // $_SERVER["DOCUMENT_ROOT"]
}
$res = json_decode(curl_exec($ch));
curl_close($ch);
if (!empty($res->server)) {
// Сохранение фото в группе.
if ($img)
{
$res = json_decode(file_get_contents(
'https://api.vk.com/method/photos.saveWallPhoto?group_id=' . $group_id
. '&server=' . $res->server . '&photo='
. stripslashes($res->photo) . '&hash='
. $res->hash . '&access_token=' . $access_token
));
}
if (!empty($res->response[0]->id)) {
// Отправляем сообщение.
$params = array(
'access_token' => $access_token,
'owner_id' => '-' . $group_id,
'from_group' => '1',
'message' => $message,
);
if ($img)
{
$params['attachments'] = $res->response[0]->id;
}
$res2 = file_get_contents(
'https://api.vk.com/method/wall.post?' . http_build_query($params)
);
}
}
}
}
Answer the question
In order to leave comments, you need to log in
"Logs" would be desirable to see when the "post" does not work.
And everything will become clear there.
In particular, the variables at each stage are:
$res =
$res2 =
$res = json_decode(file_get_contents(
'https://api.vk.com/method/photos.getWallUploadServer?group_id='
. $group_id . '&access_token=' . $access_token
));
$res = json_decode(file_get_contents(
'https://api.vk.com/method/photos.saveWallPhoto?group_id=' . $group_id
. '&server=' . $res->server . '&photo='
. stripslashes($res->photo) . '&hash='
. $res->hash . '&access_token=' . $access_token
));
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question