Одна из старых реализаций:
private function call($method, $params) {
$params['v'] = $this->v; // версия для запроса к VK API
$params['access_token'] = $this->token; // токен пользователя.
$url = "https://api.vk.com/method/{$method}?" . http_build_query($params);
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
$data = curl_exec($ch);
curl_close($ch);
$result = json_decode($data);
if($result->error) {
// self::endLog();
}
return $result;
}
public function loadPhoto($photo, $group) {
$url = self::call('photos.getUploadServer', ['album_id' => $group['album'], 'group_id' => $group['group_id']])->{"response"}->{"upload_url"};
$data = ['file1' => new CURLFile($photo)];
$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, $data);
$result = json_decode(curl_exec($ch));
$save = self::call('photos.save', ['server' => $result->server, 'photos_list' => $result->photos_list, 'album_id' => $result->aid, 'hash' => $result->hash, 'group_id' => $group['group_id']]);
return ['pid' => $save->response[0]->id, 'oid' => $save->response[0]->owner_id];
}
Сравнительно недавно, кстати, вышел официальный SDK от ВКонтакте, тоже можно использовать -
github