Post photos (and texts, links, etc.) on the wall of VK with PHP. <alebal web Blog> | Appunti di PHP, MySql, javascript, Css, HTML, HTML5 e altro...

Home PHP MySql Server Javascript GTA 5 Tristi storie Varie Open menu

Post photos (and texts, links, etc.) on the wall of VK with PHP | <alebal web Blog>

I struggled a bit, so I'll write it down before I forget.

All the mess starts with an app and a token.

To create the app, go here: https://vk.com/apps?act=manage, create it STANDALONE.

To get the token is more  a mess.

You need first the VK API, which you have to download from here: https://github.com/fdcore/vk.api

Download them, upload them to your server and include them in your script. Es:

include 'vk.api-2/src/vk.php';

Then copy paste in the script this mess here:

$v = new Vk(array(
	'client_id' => 123456, // (required) app id
	'secret_key' => '', // (required) get on https://vk.com/editapp?id=12345§ion=options
	'user_id' => 123456, // your user id on vk.com
	'scope' => 'wall,photos,friends,groups', // scope access
	'v' => '5.52' // vk api version
));

$url = $v->get_code_token();

echo $url;

You find almost all the data in the settings of the application, the user_id, you find it by visiting your profile on VK, in the url, es: https://vk.com/id123456, copy only the numbers.

All this mess prints a URL on the screen, copy it and paste it into the address bar.

If everything went well it asks you to grant the permits and it will redirects you to another page with something written in russian. You don't care what is written, what you are interested in is the token that is in the address bar of the browser.

You only need this mess once, then you can comment the code.


With all the part above, I had a lot of trouble, did not want to work and give me a token, showed me the most absurd errors.

Then one day, I deleted and created a new app (which I had already done several times in the previous days) and everything started to work, gave me a token and a minute later I published photos on VK... I don't know...

https://stackoverflow.com/questions/55753560/vk-auto-post-in-wall-via-php


At this point to upload photos on the wall of VK, copy the token and use it on this other code.

    $config['secret_key'] = '';
    $config['client_id'] = '123456';
    $config['user_id'] = '123456';
    $config['access_token'] = 'You found it on the previous URL';
    $config['scope'] = 'wall,photos,friends,groups';

    // Get a new instance of VK.
    $v = new Vk($config);

    // Define the attachment to insert, in this case an image.
    $attachments = $v->upload_photo(0, array('images/123456.jpg'));

    // Post the message and image to your wall.
    $response = $v->wall->post(array(
       'message'=>'test https://alebalwe-blog.com',
       'attachments' => implode(',', $attachments)
    ));

On $attachment, there goes the address of the photo you want to upload, or more photos, is an array. The photo must be on your server, with relative address, does not understand the absolute URLs.

On message everything you want to write, including links, do not see them as when you publish them from VK (with preview, description, etc.), but understands them and if you click it sends you on the link.

You can probably also upload the links in the Vk style, but for the moment, I'll settle.

Google Digg Reddit Tumblr Pinterest StumbleUpon Email

Rating: 5 out of 5 by 3766 visitors

Leave your comment