M
M
mdamem2018-07-30 13:16:48
PHP
mdamem, 2018-07-30 13:16:48

Attach image to post, VK API?

I can't attach an image to a post in a group.
In order:
I determine the path to the picture, pass the group ID without a minus and the token to the getWallUploadServer method

$photo_path = dirname(__FILE__).'/photo1.jpg';
    $postData = array("file1" => '@'.$photo_path);
    $getWallUploadServer = "https://api.vk.com/method/photos.getWallUploadServer?group_id=$group_id_for_method&access_token=$token&v=5.80";

We take the upload_url, pass the picture and pull out the server, photo, hash values
$response = file_get_contents($getWallUploadServer);
    $response = json_decode($response)->response->upload_url;
    
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $response);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
    $result = json_decode(curl_exec($ch));
    $server = $result->server;
    $photo = $result->photo;
    $hash = $result->hash;

Next, we pass server, hash and photo to the saveWallPhoto method and pull out the photo id from there:
$saveWallPhoto = file_get_contents("https://api.vk.com/method/photos.saveWallPhoto?access_token=$token&v=5.37&group_id=$group_id_for_method&server=$server&photo=$photo&hash=$hash");
    $attachment = json_decode($saveWallPhoto)->response[0]->id;

Well, in the final I form an image by its id and pass it to the wallPost method
header("Location: https://api.vk.com/method/wall.post?owner_id=$group_id&from_group=1&access_token=$token&attachments=photo{$group_id}_{$attachment}&v=5.37");

And I see an error
"One of the parameters specified was missing or invalid: invalid message param"

At the same time, if you open some image and take its photo parameters, for example `photo-24149106_456241216` and manually insert it into your final link, this image is perfectly fasted in the group. Apparently, my id is somehow formed incorrectly, because I don’t see where else there could be an error. If anyone has worked with this, tell me, the second day I can not figure out how to fix it.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
mdamem, 2018-07-30
@mdamem

Answer:
Now everything works.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question