K
K
kr_ilya2019-01-20 17:29:33
PHP
kr_ilya, 2019-01-20 17:29:33

How to upload a photo to a VK group using the PHP API?

I'm trying to upload a photo to the server, but it doesn't work.
This code returns NULL

$image = "/img/photo_2019-01-15_18-37-20.jpg";
$access_token = "d9e3a391cвыф9bfc0b4687e9caf1963a5d7376952f8f5839e";
$group_id = '174вфы08'; 
$res = json_decode(file_get_contents(
    'https://api.vk.com/method/photos.getWallUploadServer?group_id=176908&v=5.92&access_token=' . $access_token
));
// echo $res;
// print_r($res);
    // Отправка изображения на сервер.
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $res->response->upload_url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    "Content-Type:multipart/form-data"
));
    curl_setopt($ch, CURLOPT_POSTFIELDS, array('photo' => new CURLFile($image)));

    $res = json_decode(curl_exec($ch));
    curl_close($ch);
    // echo $res;
    print_r($res);
    var_dump($res);

And this
$image = "/img/photo_2019-01-15_18-37-20.jpg";
$access_token = "d9e3a391cвыф9bfc0b4687e9caf1963a5d7376952f8f5839e";
$group_id = '174вфы08'; 
$res = json_decode(file_get_contents(
    'https://api.vk.com/method/photos.getWallUploadServer?group_id=17908&v=5.92&access_token=' . $access_token
));
// echo $res;
// print_r($res);
    // Отправка изображения на сервер.
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $res->response->upload_url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    "Content-Type:multipart/form-data"
));
    curl_setopt($ch, CURLOPT_POSTFIELDS, array('photo' => $image));

    $res = json_decode(curl_exec($ch));
    curl_close($ch);
    // echo $res;
    print_r($res);
    var_dump($res);

Returns this
stdClass Object
(
    [server] => 846523
    [photo] => []
    [hash] => b8cd31be33cde372162b7403d1d51feb
)

What is the problem?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
shitprog, 2019-01-22
@kr_ilya

read the documentation for the API loading is carried out in 2 steps:
1) obtaining the server address
2) directly sending data

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question