Hello. I work with the vk API, send messages using the ordinary messages.send method, using this code:

 file_get_contents("http://api.vk.com/method/messages.send?message=".urlencode($message)."&v=5.60&peer_id=$pid&access_token=$token");` 

Everything works fine if I send short messages (the maximum length of the message I managed to transmit was ~ 850 characters). Considering the fact that after urlencode() each Russian letter = 3 characters, the length of the url is pretty big. But when I send longer messages, php returns warning: file_get_contents ... I tried to send a request through mozilla, but it did not even work in it (it says something like it was not possible to establish a connection). If you try to send such a long text from the VC website - everything is fine, VC allows you to send messages of such length.

Therefore, the problem is in a long url. How can I send a request with such a long message in a different way?

  • What did firefox return? Interested in Status Code. - Andrei Arshinov
  • CURL probovali? - L. Vadim
  • @AndreyArshinov, http answer 400. - LNK
  • @ L.Vadim, now I tried curl, but there, too, in CURLOPT_URL I give a long url. An empty string comes in response. - LNK

1 answer 1

Send requests using cURL using the POST method like this:

 $parameters = [ 'access_token' => $token, 'v' => '5.60', 'peer_id' => $pid, 'message' => $message ]; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, 'https://api.vk.com/method/messages.send'); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($parameters)); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // в этой переменной JSON-объект, который вернет ВК $curl_result = curl_exec($ch); curl_close($ch); 

now I tried curl, but there, too, in CURLOPT_URL, I pass a long url. A blank line is returned.

This is because you are pushing data in the GET parameters as well, but in the body of the POST request via the CURLOPT_POSTFIELDS parameter.

  • Thank you very much! I just did not know that the vk API can also work with POST requests. Now everything works. - LNK