R
R
rusgayfer2017-07-23 03:08:11
In contact with
rusgayfer, 2017-07-23 03:08:11

How to output JSON API VK?

There is a code:

// Уведомления через ВК POST запросом
                      $url = 'https://api.vk.com/method/messages.send';
                      $params = array(
                      'user_ids' => $idadmins.''.$ownerId,    // Кому отправляем
              'title' => 'Новое сообщение: '.$admins['response'][0]['name'],   // Заголовок
                      'message' => $message,   // Что отправляем
                      'attachment' => $attachments, // Прикрепления
                      'access_token' => $tokengroup,  // access_token 
                      'v' => '5.67',
                                     );

                      // В $result вернется id отправленного сообщения
                      $request = file_get_contents($url, false, stream_context_create(array(
                      'http' => array(
                      'method'  => 'POST',
                      'header'  => 'Content-type: application/x-www-form-urlencoded',
                       'content' => http_build_query($params)
                                      )
                       )));

You need to output this to json to display the error
Here is my error output code but it does not work because there is no jSON
if (isset($request['error']['error_code'])) {
                       $errorcode = "<div class=\"alert alert-danger alert-dismissible\"> Ошибка при уведомлении сообщений. Не можем отправить без разрешения!</div>"; 
                                      }

But if I do this:
$request = json_decode(file_get_contents("https://api.vk.com/method/messages.send?user_ids={$ownerId}&title={$groupname}&message={$message}&attachment={$attachments}&access_token={$tokengroup}"),true);

An error is thrown if the notification is not sent.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Serezha, 2017-07-23
@rusgayfer

Well... First, put the query result in a separate variable, put the json decode result in another separate variable, search for errors on it, and output the first variable that you still have a json string.
Or I didn't understand the question.
UPD: the author, you once have json. You need an array of objects, not json. Understand at least in terms for starters.
What's the problem then? With a get request, you provide an example wrapped in json_decode, so do the same with the result from the post request.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question