<?php
$token = 'TOKEN';
$group_id = 'GID'; //без минуса
$album = 'AID';
$imgName = 'РЕАЛЬНЫЙ ПУТЬ ДО ФОТО ЧТОБ ФАЙЛ ОТКРЫВАЛСЯ ПО ЭТОМУ АДРЕСУ';
$vk = new Vk($token);
$upload_server = $vk->request('photos.getUploadServer', [
'album_id' => $album,
'group_id' => $group_id,
]);
$upload = $vk->uploadFile($upload_server['upload_url'], $imgName);
$i = $vk->request('photos.save', [
'server' => $upload['server'],
'photos_list' => $upload['photos_list'],
'aid' => $upload['aid'],
'hash' => $upload['hash'],
'album_id' => $album,
'group_id' => $group_id,
]);
class Vk
{
private $token;
private $v = '5.00';
public function __construct($token)
{
$this->token = $token;
}
public function uploadFile($url, $path)
{
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, true);
if (class_exists('\CURLFile')) {
curl_setopt($ch, CURLOPT_POSTFIELDS, ['file1' => new \CURLFile($path)]);
} else {
curl_setopt($ch, CURLOPT_POSTFIELDS, ['file1' => "@$path"]);
}
$data = curl_exec($ch);
curl_close($ch);
return json_decode($data, true);
}
public function request($method, array $params = [])
{
$params['v'] = $this->v;
$ch = curl_init('https://api.vk.com/method/' . $method . '?access_token=' . $this->token);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, true);
if ($params) {
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
}
$data = curl_exec($ch);
curl_close($ch);
$json = json_decode($data, true);
if (!isset($json['response'])) {
throw new \Exception($data);
}
usleep(mt_rand(1000000, 2000000));
return $json['response'];
}
}
public function uploadFile($url, $path)
{
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, true);
if (class_exists('\CURLFile')) {
curl_setopt($ch, CURLOPT_POSTFIELDS, ['file' => new \CURLFile($path)]);
} else {
curl_setopt($ch, CURLOPT_POSTFIELDS, ['photo' => "@$path"]);
}
$data = curl_exec($ch);
curl_close($ch);
return json_decode($data, true);
}