D
D
Diversia2017-08-17 11:29:36
PHP
Diversia, 2017-08-17 11:29:36

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

1 answer(s)
U
userfordownload, 2017-08-17
@userfordownload

"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  =

It is better not to overwrite them, but to write them into new ones, for example
$res = json_decode(file_get_contents(
    'https://api.vk.com/method/photos.getWallUploadServer?group_id='
    . $group_id . '&access_token=' . $access_token
  ));

$_UploadServer = json_deco....
$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
          ));

$_saveWallPhoto = json_decode(file_get_conte......
And it will be clear from the logs where the code falls off, otherwise you just have to guess.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question