Having received the task to create an automatic publication of the site materials on the wall of our Vkontakte page, I found that there are relatively few recipes on this seemingly relevant topic.
In fact, everything turned out to be quite simple, and reading the API documentation often helps save time.
')
In order to access the contact API from a php script, you need to start by clicking on the “Connect Website” button on the
developer page.
On the following form, select the “Standalone application” option and enter a name, for example, Auto app. After clicking on the button "Connect application" you will have to enter the code. He will come in a minute via SMS to a mobile phone specified in the user profile under which you are logged in. Great, now you have a standalone application, information about which you can later edit on the "Applications" page by clicking on the "settings" link.
On the new page do not forget to enter the real address of the site, the base domain and save the changes; if you leave these fields blank, the contact will not publish your "automatic" posts.

Now, after the application is created, we can get 2 important values ​​for the application configuration - the
protected key and the
application ID that we need to work.
Create an application folder on your project, for example, my_folder /.
For convenience, we will also create 3 files, of which our standalone application will consist of:
onfig.php - plugin config, in which we put the protected key and application ID, as well as the user ID, on the wall of which we will publish our posts.
connect.php is a simple page for the standard procedure for connecting an application to a user page.
export.php is a script that will directly post our materials.
This is how the complex code of our configuration file will look like:
<?php * standalone * ID , */ $vkontakteApplicationId = '12345'; $vkontakteKey =' ';
Now we need to
connect the application to the user’s Vkontakte page using an OAuth 2.0-based authorization mechanism. The connection is as follows:
- We create a link in a special way (see below) on the connect.php page of our application.
- Open the browser and log in Vkontakte under the user on whose wall we will post messages. This may well be the same user under which we registered the application.
- Go to your connect.php page and click on the link, go to the Vkontakte page.
- On the Vkontakte page, we authorize the application (by clicking the “Allow” button), after which the Vkontakte redirects the browser back to the connect.php page, passing the code parameter via the url
- Using the code parameter, we make a request to the contact using the https protocol and get access_token - the token that we will save in a text file (for example) and will be used later when publishing messages on the wall. In the process of publishing to authorize the application during data transfer, we no longer need the secret key and id of our application, and the code parameter. We will only use the received token.
Here is a simple version of the connect.php page:
<?php require 'config.php'; if (!empty($_GET['code'])){
In order for the json_decode (file_get_contents ('https: // ...')) construction to work, php 5.xc version must be installed on the server with openssl support.
The query string of the link “authorization Vkontakte” is composed of the following parameters:
client_id - ID of our application Vkontakte, which we have already written in the configuration file;
scope - the requested
application access rights , which can be separated by commas. In this case, the application requests access to the API from a third-party server at any time (offline) and wall - access to the methods of working with the wall;
redirect_uri - an absolute link to the connect.php connection page, to which the code parameter will be passed;
responce_type - code.
Now, after the access_token is received, we can proceed with the publication of materials on the wall.
<?php require 'config.php'; $vkontakteAccessToken = file_get_contents('token.txt');
We run the script, and if we are not mistaken anywhere, we get the following result:

Similarly, you can form queries to use other API methods. The query string is built like this:
api.vkontakte.ru/method + method name + user ID in contact + previously received token + parameter list of the corresponding
API method.
In the above example, the required parameter is message - this is the actual text posted on the wall of Vkontakte. The text should be in UTF-8 encoding; if you are still working with windows-1251, use this conversion:
$text = urlencode(iconv( 'windows-1251', 'utf-8' , $text));
Since our text and link are part of the url query string, do not forget to use urlencode encoding.
If we want to post only the text, without reference, then, if successful, the server will send an answer of this type
{"Response": {"post_id": 1}}
In order for a link to be posted next to a message on the wall, attachment = $ link must be added to the request line, where $ link is an absolute link to an existing page on your site. In this case, the post is published postponed (in a few seconds, less than a second), and the server receives a response like
{"Response": {"processing": 1}}
You can read more about the wall.post method on
the documentation page .
Do not send requests to the server too often. In most cases, the limit of one request per minute is sufficient, otherwise the server will return the error “Captcha is needed” to you and the publication will be temporarily suspended.
The result of a standalone application looks like
this.Here, everything turned out to be quite simple, we put the export script on cron, and the content manager will not have to post entries manually!