Answer the question
In order to leave comments, you need to log in
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);
$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);
stdClass Object
(
[server] => 846523
[photo] => []
[hash] => b8cd31be33cde372162b7403d1d51feb
)
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question