📜 ⬆️ ⬇️

Sending messages through the VK API using PHP for the lazy

What we will do: we will configure sending messages on behalf of a particular user to several chats and to other users using an HTTP request using PHP , with minimal effort.

Actually, we will not limit ourselves to some messages. Everything will work through the standalone vk application by the principle that we can potentially do all the actions that will be available to this user (more precisely, everything that we will issue to ourselves, but more on that later).

We consider messages for the very reason that you can work with them only in stand-alone applications.
')
Action plan:
1. Create an application
2. Get access_token
3. send messages

1. Create an application


This item should be made under the account to which the application will be associated (Not necessarily the same one from which messages will come).

Go here vk.com/dev , click to create an application .

image

We write the name of any kind - it will not appear anywhere.
Type - Standalone application.

Next, you need to enter the code from the SMS that will come to the number associated with the account. After entering the code, we change nothing in the fields, go to the settings and copy the application ID.

image

More then you can not touch anything.


2. Using the ID and secret_key from the application, we get access_token


access_token - a string of a certain number of characters that will be sent with a POST request to perform some functions. Actually, it is tied to it, on behalf of which user we will act and what we can do.

To do this, create a page on the site with this code:
<? $client_id = '1111111'; $scope = 'offline,messages' ?> <a href="https://oauth.vk.com/authorize?client_id=<?=$client_id;?>&display=page&redirect_uri=https://oauth.vk.com/blank.html&scope=<?=$scope;?>&response_type=token&v=5.37">Push the button</a> 

$ client_id - in fact, the ID of our application from paragraph 1.

$ scope - the list of rights that we can do on behalf of our user. Konkternno, for our task is suitable exactly this: the ability to access at any time (offline), and access to messages (messages). If you need more, the full list is here . You can set either a bit mask or list the names.

Next, an important point : We go to our page with the button, being logged in the VC under the user, on behalf of whom we will work, and zamkayem button. A similar query will appear:

image

In addition to the item access to general information , all other items will correspond to the list of permissions that were transferred to the scope. We press further and see the following:

image

Acces_token is highlighted in red, for which the entire second paragraph was written.


3. HTTP requests, sending parameters via POST, send messages or use any other API methods



For impatient , check that everything works as follows:

Replace the zeros at the end of our acces_token and follow this link in the browser.
 https://api.vk.com/method/messages.send?user_id=6269901&message=habrahabr&v=5.37&access_token=000000 

What will happen:
A message with the text from the message parameter will be sent to the user with the specified user_id . (If you leave it as it is, it will come to me. I will not be offended).

Recipient parameters:


Other options for sending messages.

The full list of methods (with the scope = offline parameter , messages ) will work, respectively, only with the message section.

And finally, the function to send a message through POST. Simple as a Kalashnikov assault rifle - just for clarity:
 function send($id , $message) { $url = 'https://api.vk.com/method/messages.send'; $params = array( 'user_id' => $id, //   'message' => $message, //   'access_token' => '0000000000000000000000000000', // access_token   ,         'v' => '5.37', ); //  $result  id   $result = 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) ) ))); } 



Finally, all links:

Developer Page
Create application
List of possible permissions
All methods
Posting options

That's it.

Source: https://habr.com/ru/post/265563/


All Articles