0

I would like to post on my wall in vk.com some photos, texts, etc via PHP. From a couple of days I'm trying to understand the official guides of VK (https://vk.com/dev/PHP_SDK) and some scipt found in GitHub (https://github.com/fdcore/vk.api).

There are few lines of code but I struggle to get to the second or third line.

I get immediately php errors, JSON errors, etc etc etc. I can't even make myself give the first Access_token, Autorization_code etc. I admit I don't understand much about OAuth and API, but here I can't even begin.

Even the names of things are strange, secret_key, secure_key, api_key, I think they are always the same, but I'm not sure anymore, but they one gives me, I think it's her.

Do these APIs work? Is there a guide for dummy? That you guide me step-by-step, just to understand if I did something wrong, even if I made very few steps.

Or maybe there is some other script or class that works?

alebal
  • 5,838
  • 3
  • 10
  • 28
  • If you have written some codes and get some errors, you should put them in the question. If you just ask "Do these APIs work?" the answer is YES. The official guide looks very detailed. – shingo Apr 24 '19 at 03:15

2 Answers2

0

I'm guessing as a start, you will need code similar to below, not forgetting about downloading the VK class library from Github https://github.com/fdcore/vk.api/blob/v2/src/vk.php.

<?php

    // Need to have the vk.php in the same directory.
    include 'vk.php';

    // Please complete the below with your details/credentials.
    $config['secret_key'] = '';
    $config['client_id'] = '';
    $config['user_id'] = '';
    $config['access_token'] = '';
    $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('1737759.jpg'));

    // Post the message and image to your wall.
    $response = $v->wall->post(array(
       'message'=>'test 1737759.jpg',
       'attachments' => implode(',', $attachments)
    ));

?>

The class deals with all of the behind the scene coding and talking to Vk as a whole. And why reinvent the wheel!

Jim Grant
  • 1,128
  • 2
  • 13
  • 31
  • This say: Fatal error: Uncaught VkException: Application authorization failed: method is unavailable with service token... I don't have access_token, I try use service_token that I have in developers area... probably wrong... How should I get an access_token? – alebal Apr 26 '19 at 22:36
  • I try the example code to get an access_token, give me this url: https://oauth.vk.com/authorize?response_type=token&client_id=#######&redirect_uri=https%3A%2F%2Foauth.vk.com%2Fblank.html&display=page&version=5.35&scope=offline%2Cwall, when I go to the url: error "invalid_request" error_description "invalid scope"... what's wrong? – alebal Apr 26 '19 at 22:44
  • If i try only: scope=wall, error "invalid_request" error_description "source_url is incorrect, check app settings: " – alebal Apr 26 '19 at 22:46
  • The access token comes from a previous VK call, please see the following answer: https://stackoverflow.com/questions/25619028/how-to-get-an-access-token-from-vkontakte-vk-via-oauth2 – Jim Grant Apr 29 '19 at 09:40
  • I tried all the links and possible solutions that they suggested, but I never see this damn token... And they don't seem to be able to make it work either. Seems like an extremely stupid thing, why doesn't it work? – alebal Apr 29 '19 at 20:41
  • https://oauth.vk.com/authorize?client_id=123...&scope=wall,offline&redirect_uri=https://www.example.com/oauth/vk.php&display=page&v=5.35&response_type=token https://oauth.vk.com/authorize?client_id=123..&scope=wall,offline&redirect_uri=https://oauth.vk.com/blank.html&display=page&v=5.35&response_type=token https://oauth.vk.com/authorize?client_id=123..&scope=wall,offline&redirect_uri=https://oauth.vk.com/oauth/vk.php&display=page&v=5.35&response_type=token https://oauth.vk.com/authorize?client_id=123..&scope=wall,offline&redirect_uri=https://oauth.vk.com/blank.html&response_type=token – alebal Apr 29 '19 at 20:42
0

Everybody stop!

I deleted the application and I made a new one... and it works... I have a working URL and a working token.

However it is strange, these days I have done many tests tried many scripts and created and deleted apps several times always with the same settings (Standalone)... Just now he liked it and it worked...

Now with the code that gave me Jim Grant and the token, everything works, I can upload photos on the wall of VK via PHP.

To have the URL with the token I used this:

$v = new Vk(array(
    'client_id' => 123456, // (required) app id
    'secret_key' => '', // (required) get on https://vk.com/editapp?id=12345&section=options
    'user_id' => 12345, // 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;

Then I granted the permissions and took the token from the URL in the address bar.

And then I used the Jim Grant code with the new token, and everything works. Really simple, when everything works.

alebal
  • 5,838
  • 3
  • 10
  • 28
  • https://alebalweb-blog.com/79-post-photos-and-texts-links-etcon-the-wall-of-vk-with-php.html I wrote a tutorial for dummy before I forget. – alebal Apr 29 '19 at 22:23