A
A
andrey_levushkin2019-02-08 15:27:37
PHP
andrey_levushkin, 2019-02-08 15:27:37

The script for uploading a photo to the album of the VKontakte group does not work correctly. What to do?

This script should upload a photo to the VK group album. On the video, everything works for a person:
When I run it, I get errors:
Notice: Undefined property: stdClass::$response in /test.php on line 14
14 line of code:
$url = json_decode($url)->response->upload_url;
And error:
Notice: Trying to get property of non-object in /test.php on line 14
Array ( [error] => Array ( [error_code] => 8 [error_msg] => Invalid request: v (version) is required [request_params] => Array ( [0] => Array ( [key] => oauth [ value] => 1 ) [1] => Array ( [key] => method [value] => photos.save ) [2] => Array ( [key] => server [value] => ) [3] => Array ( [key] => photos_list [value] => ) [4] => Array ( [key] => album_id [value] => ) [5] => Array ( [key] => hash [value ] => ) [6] => Array ( [key] => gid [value] => group_id here ) ) ) )
The script itself:

<?php
// Только STANDALONE TOKEN
$token = 'token';

$group_id = 'id';
$album_id = 'id';
$v = '5.62'; //версия vk api
$image_path = dirname(__FILE__).'/111.jpg';//путь до картинки
$post_data = array("file1" => '@'.$image_path);

// получаем урл для загрузки
 $url = file_get_contents("https://api.vk.com/method/photos.getUploadServer?album_id=".$album_id."&group_id=".$group_id."&v=".$v."&access_token=".$token);
$url = json_decode($url)->response->upload_url;
//print_r($url);

// отправка post картинки
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
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, $post_data);
$result = json_decode(curl_exec($ch),true);
//print_r($result);

// сохраняем
$safe = file_get_contents("https://api.vk.com/method/photos.save?server=".$result['server']."&photos_list=".$result['photos_list']."&album_id=".$result['aid']."&hash=".$result['hash']."&gid=".$group_id."&access_token=".$token);
$safe = json_decode($safe,true);
print_r($safe);
// итог
?>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Soslan Khloev, 2019-02-08
@hloe0xff

$url = json_decode($url)->response->upload_url;
$url = json_decode($url)->upload_url;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question