A
A
Alexander Maslov2016-09-20 13:24:22
PHP
Alexander Maslov, 2016-09-20 13:24:22

How to get the correct request from the VK Community API?

Good day! I encountered such a problem when working with the VK API . I
use the API for communities. It is necessary to pull out dialogs in which there are unread messages.
There is a function:

public function request($method,$params=array())
    {
        $uri = 'https://api.vk.com/method/'.$method;
        $params['access_token'] = $this->token;
        $params['v'] = '5.53';
            return json_decode(file_get_contents($uri.'?'.http_build_query($params)),true);
    }

public function unread()
{ 
    return  $response = $this->request('messages.getDialogs',
[
        'count'=>'20',
        'unread'=>'1',
        'offset'=>'-20',
        'start_message_id'=>1
]);
    }

When calling unread(), as a result I get a response from the VK server
[
  "response" =>  [
    "count" => 12
    "real_offset" => 12
    "unread_dialogs" => 1
    "items" => []
  ]
]

items should contain just a dialog with an unread message, as indicated by unread_dialogs , but it's empty... Ok, let's go to Postman and check the request there. Creating a link:

https://api.vk.com/method/messages.getDialogs?v=5.53&access_token={API_TOKEN}&count=20&offset=-20&unread=1&start_message_id=1

We put down all the values ​​\u200b\u200bthat were used above, I get the answer:
{
  "response": {
    "count": 1,
    "real_offset": -8,
    "items": [
      {
        "unread": 1,
        "message": {
          "id": 150,
          "date": 1474366312,
          "out": 0,
          "user_id": 01111111,
          "read_state": 0,
          "title": " ... ",
          "body": "проверка"
        },
        "in_read": 149,
        "out_read": 149
      }
    ]
  }
}

As you can see, everything is fine, what was required, then I got it.
And now the question is, what am I doing wrong, and why is there such a difference when calling?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alex, 2016-09-20
@Kozack

1. Try to write like this:

public function unread()
{ 
    return  $response = $this->request('messages.getDialogs', array(
        'count'=>'20',
        'unread'=>'1',
));

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question