├── README.md ├── config.json └── src ├── ConfigParser.php ├── VkAccount.php ├── VkApps.php ├── VkAudios.php ├── VkBot.php ├── VkException.php ├── VkMessages.php └── VkWall.php /README.md: -------------------------------------------------------------------------------- 1 | # VkBot 2 | Create your boat with ease / С лёгкостью создай своего бота во ВКонтакте! 3 | ### Описание 4 | Этот проект размещает в себе не только код для создания бота на платформе социальной сети ВКонтакте, но и включает другие возможности для реализации других затей с помощью VK API и этой библиотеки. 5 | 6 | ### Требования 7 | Наличие PHP 7 версии 8 | 9 | ### Поддержка 10 | Если у вас есть предложения помощи и вы хотите помочь нам в развитии данного проекта, вы можете создать Pull Request с предложенными изменениями. 11 | 12 | ### Установка и первый запуск 13 | Для начала загрузите проект с исходными кодами, используя команду ```git clone git://github.com/xpyctum/VkBot.git``` и отредактируйте config.json. 14 | 15 | ### Пример запуска 16 | Предлагаем ознакомиться с небольшим примером кода, с помощью которого вы можете запустить наш инструмент. 17 | ```php 18 | getMessagesApi()->getUnreadMessages()); // Покажет непрочитанные сообщения 22 | ``` 23 | С другими примерами вы можете ознакомиться в Wiki проекта. 24 | 25 | ### RoadMap: 26 | - [x] Exception 27 | - [x] Основной класс 28 | - [x] ConfigParser 29 | - [ ] Обьекты информации 30 | - [ ] Messages API 31 | - [ ] Videos API 32 | - [ ] Wall API -------------------------------------------------------------------------------- /config.json: -------------------------------------------------------------------------------- 1 | { 2 | //Токен, полученный при авторизации приложения. 3 | "token_key": "", 4 | 5 | //ID приложения 6 | "app_id": "", 7 | 8 | //Защищенный ключ приложения 9 | "security_key": "", 10 | 11 | //Таймаут 12 | "timeout": 5, 13 | 14 | //Версия VK API 15 | "api_version": 5.24, 16 | 17 | /** 18 | * Разрешения/права приложения. Перечислять через запятую 19 | * Подробнее: https://vk.com/dev/permissions 20 | */ 21 | "scopes": 22 | [ 23 | "offline", 24 | "" 25 | ], 26 | 27 | //======== AntiCaptcha Part ========== 28 | 29 | //Сервис антикапчи. (По умолчанию выключен.) 30 | "anticaptcha_enabled": false, 31 | 32 | /** 33 | * Антикапча, сервис, который позволяет справиться с назойливой капчей. 34 | * Поддержка antigate.com, rucaptcha.ru встроена 35 | * Подробнее с этим можно ознакомиться в Wiki страницах 36 | */ 37 | "anticaptcha_service": "rucaptcha.ru", 38 | 39 | //Ключ доступа для работы с сервисом антикапчи 40 | "captcha_key":"", 41 | 42 | //Сохранение изображений капчи (и не удалять. Если false, будут удаляться после отправления на сервис) 43 | "captcha_save_images": true, 44 | 45 | //Папка для сохранений изображений капчи. Тажке у папки должны быть права записи. (775,777) 46 | "captcha_images_folder": "captcha_images" 47 | 48 | } 49 | -------------------------------------------------------------------------------- /src/ConfigParser.php: -------------------------------------------------------------------------------- 1 | loadConfig($path); 9 | $this->checkConfig(); 10 | } 11 | 12 | private function loadConfig($path){ 13 | $this->config = json_decode(file_get_contents($path),true); 14 | } 15 | 16 | private function checkConfig(){ 17 | if(!isset($this->config['token_key'])){ 18 | throw new VkException('Токен не найден', 10001); 19 | } 20 | 21 | if(!isset($this->config['app_id'])){ 22 | throw new VkException('APP ID не найден', 10002); 23 | } 24 | 25 | if(!isset($this->config['security_key'])){ 26 | throw new VkException('Защищённый ключ не найден', 10003); 27 | } 28 | 29 | if(!isset($this->config['scopes'])){ 30 | throw new VkException('Разрешения не найдены', 10004); 31 | } 32 | 33 | if(!isset($this->config['api_version'])){ 34 | throw new VkException('Версия API не найдена', 10005); 35 | } 36 | 37 | if(!isset($this->config['timeout'])){ 38 | throw new VkException('Timeout не найден', 10006); 39 | } 40 | } 41 | 42 | public function getToken() : string { 43 | return $this->config['token_key']; 44 | } 45 | 46 | public function getAppId() : int { 47 | return $this->config['app_id']; 48 | } 49 | 50 | public function getSecurityKey() : string { 51 | return $this->config['security_key']; 52 | } 53 | 54 | public function getScopes() : array { 55 | return $this->config['scopes']; 56 | } 57 | 58 | public function getApiVerion() : double { 59 | return $this->config['api_version']; 60 | } 61 | } -------------------------------------------------------------------------------- /src/VkAccount.php: -------------------------------------------------------------------------------- 1 | getAppPermissions(); 31 | if(($perms&1)>0) $this->notify = true; 32 | if(($perms&2)>0) $this->friends = true; 33 | if(($perms&4)>0) $this->photos = true; 34 | if(($perms&8)>0) $this->audio = true; 35 | if(($perms&16)>0) $this->video = true; 36 | if(($perms&32)>0) $this->offers = true; 37 | if(($perms&64)>0) $this->questions = true; 38 | if(($perms&128)>0) $this->pages = true; 39 | if(($perms&256)>0) $this->menu = true; 40 | if(($perms&1024)>0) $this->status = true; 41 | if(($perms&2048)>0) $this->notes = true; 42 | if(($perms&4096)>0) $this->messages = true; 43 | if(($perms&8192)>0) $this->wall = true; 44 | if(($perms&32768)>0) $this->ads = true; 45 | if(($perms&65536)>0) $this->offline = true; 46 | if(($perms&131072)>0) $this->docs = true; 47 | if(($perms&262144)>0) $this->groups = true; 48 | if(($perms&524288)>0) $this->notifications = true; 49 | if(($perms&1048576)>0) $this->stats = true; 50 | if(($perms&4194304)>0) $this->email = true; 51 | if(($perms&134217728)>0) $this->market = true; 52 | } 53 | 54 | /** https://vk.com/dev/account.getCounters 55 | * @param $filter 56 | * @return array 57 | */ 58 | public function getCounters($filter){ 59 | return $this->api("account.getCounters",["filter" => $filter]); 60 | } 61 | 62 | /** https://vk.com/dev/account.setNameInMenu 63 | * @param $user_id 64 | * @param $name 65 | * @return array 66 | */ 67 | public function setNameInMenu($user_id,$name){ 68 | return $this->api("account.setNameInMenu",["user_id" => $user_id,"name" => $name]); 69 | } 70 | 71 | /** https://vk.com/dev/account.setOnline 72 | * @param $voip 73 | * @return array 74 | */ 75 | public function setOnline($voip = false){ 76 | return $this->api("account.setOnline", [ "voip" => ($voip == true ? 1 : 0) ]); 77 | } 78 | 79 | /** https://vk.com/dev/account.setOffline 80 | * @return array 81 | */ 82 | public function setOffline(){ 83 | return $this->api("account.setOffline"); 84 | } 85 | 86 | //TODO 87 | /** https://vk.com/dev/account.lookupContacts 88 | * 89 | */ 90 | public function lookupContacts(){} 91 | 92 | //TODO 93 | /** https://vk.com/dev/account.registerDevice 94 | * 95 | */ 96 | public function registerDevice(){} 97 | 98 | //TODO 99 | /** https://vk.com/dev/account.unregisterDevice 100 | * 101 | */ 102 | public function unregisterDevice(){} 103 | 104 | //TODO 105 | /** https://vk.com/dev/account.setSilenceMode 106 | * 107 | */ 108 | public function setSilenceMode(){} 109 | 110 | //TODO 111 | /** https://vk.com/dev/account.getPushSettings 112 | * 113 | */ 114 | public function getPushSettings(){} 115 | 116 | //TODO https://vk.com/dev/account.setPushSettings 117 | /** 118 | * 119 | */ 120 | public function setPushSettings(){} 121 | 122 | /** https://vk.com/dev/account.getAppPermissions 123 | * @param null $user_id 124 | * @return array 125 | */ 126 | function getAppPermissions($user_id = null){ 127 | return $this->api("account.getAppPermissions",(!is_null($user_id) ? ["user_id" => $user_id] : [] )); 128 | } 129 | 130 | //TODO 131 | /** https://vk.com/dev/account.getActiveOffers 132 | * 133 | */ 134 | public function getActiveOffers(){} 135 | 136 | //TODO 137 | /** https://vk.com/dev/account.banUser 138 | * 139 | */ 140 | public function banUser(){} 141 | 142 | //TODO 143 | /** https://vk.com/dev/account.unbanUser 144 | * 145 | */ 146 | public function unbanUser(){} 147 | 148 | //TODO 149 | /** https://vk.com/dev/account.getBanned 150 | * 151 | */ 152 | public function getBanned(){} 153 | 154 | //TODO 155 | /** https://vk.com/dev/account.getInfo 156 | * 157 | */ 158 | public function getInfo(){} 159 | 160 | //TODO 161 | /** https://vk.com/dev/account.setInfo 162 | * 163 | */ 164 | public function setInfo(){} 165 | 166 | //TODO 167 | /** https://vk.com/dev/account.changePassword 168 | * 169 | */ 170 | public function changePassword(){} 171 | 172 | //TODO 173 | /** https://vk.com/dev/account.getProfileInfo 174 | * 175 | */ 176 | public function getProfileInfo(){} 177 | 178 | //TODO 179 | /** https://vk.com/dev/account.saveProfileInfo 180 | * 181 | */ 182 | public function saveProfileInfo(){} 183 | } -------------------------------------------------------------------------------- /src/VkApps.php: -------------------------------------------------------------------------------- 1 | api("apps.get",$r); 39 | } 40 | 41 | 42 | //TODO 43 | /** https://vk.com/dev/apps.sendRequest 44 | * 45 | */ 46 | public function sendRequest(){} 47 | 48 | 49 | //TODO 50 | /** https://vk.com/dev/apps.deleteAppRequests 51 | * 52 | */ 53 | public function deleteAppRequests(){} 54 | 55 | 56 | //TODO 57 | /** https://vk.com/dev/apps.getFriendsList 58 | * 59 | */ 60 | public function getFriendsList(){} 61 | 62 | 63 | //TODO 64 | /** https://vk.com/dev/apps.getLeaderboard 65 | * 66 | */ 67 | public function getLeaderboard(){} 68 | 69 | 70 | //TODO 71 | /** https://vk.com/dev/apps.getScore 72 | * 73 | */ 74 | public function getScore(){} 75 | 76 | } -------------------------------------------------------------------------------- /src/VkAudios.php: -------------------------------------------------------------------------------- 1 | "Rock", 2 => "Pop", 3 => "Rap & Hip-Hop", 4 => "Easy Listening", 5 => "Dance & House", 6 => "Instrumental", 7 => "Metal", 21 => "Alternative", 8 => "Dubstep", 9 => "Jazz & Blues", 10 => "Drum & Bass", 11 => "Trance", 12 => "Chanson", 13 => "Ethnic", 14 => "Acoustic & Vocal", 15 => "Reggae", 16 => "Classical", 17 => "Indie Pop", 19 => "Speech", 22 => "Electropop & Disco", 18 => "Other" 11 | ]; 12 | 13 | public function __construct(){ 14 | parent::__construct(); 15 | } 16 | 17 | /** 18 | * @param $id 19 | * @deprecated 20 | * @return bool 21 | */ 22 | public function getNameGenreById($id){ 23 | if(isset($this->genres[$id])){ 24 | return $this->genres[$id]; 25 | }else{ 26 | return false; 27 | } 28 | } 29 | 30 | /** 31 | * @param $genre_id 32 | * @return string|null 33 | */ 34 | public function getNameGenre($genre_id){ 35 | if(isset($this->genres[$genre_id])){ 36 | return $this->genres[$genre_id]; 37 | }else{ 38 | return null; 39 | } 40 | } 41 | 42 | /** 43 | * @param $name 44 | * @deprecated 45 | * @return bool|int|string 46 | */ 47 | public function getIdGenreByName($name){ 48 | foreach($this->getGenres() as $id => $genre_name){ 49 | if(strtolower($genre_name) == strtolower($name)){ 50 | return $id; 51 | } 52 | } 53 | return false; 54 | } 55 | 56 | /** 57 | * @param $genre_name 58 | * @return int|null 59 | */ 60 | public function getIdGenre($genre_name){ 61 | foreach($this->getGenres() as $id => $genre){ 62 | if(strtolower($genre_name) == strtolower($genre)){ 63 | return $id; 64 | } 65 | } 66 | return null; 67 | } 68 | 69 | /** 70 | * @return array 71 | */ 72 | public function getGenres(){ 73 | return $this->genres; 74 | } 75 | 76 | /** 77 | * @param int $owner_id - ID of the user or community that owns the audio file. Use a negative value to designate a community ID. (current user id is used by default) 78 | * @param int $album_id - Audio album ID. 79 | * @param string $audio_ids - IDs of the audio files to return. (list of comma-separated positive numbers) 80 | * @param bool $need_user - true — to return information about users who uploaded audio files (default: true) 81 | * @param int $offset - Offset needed to return a specific subset of audio files. (default: 0) 82 | * @param int $count - Number of audio files to return. (default: 6000, max: 6000) 83 | * @throws VkException 84 | * @return array 85 | */ 86 | public function getAudiosList($owner_id = null, $album_id, $audio_ids = '', $need_user = true, $offset = 0, $count = 6000){ 87 | $r = array( 88 | "album_id" => $album_id, 89 | "audio_ids" => $audio_ids, 90 | "need_user" => ($need_user ? "1" : "0"), 91 | "offset" => $offset, 92 | "count" => $count 93 | ); 94 | if(!is_null($owner_id)) $r["owner_id"] = $owner_id; 95 | return $this->api('audio.get',$r); 96 | } 97 | 98 | /** 99 | * @param string $request - Audio file IDs, in the following format: {owner_id}_{audio_id} (list comma-separated strings) 100 | * @throws VkException 101 | * @return array 102 | */ 103 | public function getAudiosById($request){ 104 | return $this->api("audio.getById",array("audios" => $request)); 105 | } 106 | 107 | /** 108 | * @param int $lyrics_id - Lyrics ID. 109 | * @throws VkException 110 | * @return array 111 | */ 112 | public function getLyrics($lyrics_id){ 113 | return $this->api("audio.getLyrics",array("lyrics_id" => $lyrics_id)); 114 | } 115 | 116 | /** 117 | * @param string $q - Search query string (e.g., The Beatles). 118 | * @param bool $auto_complete - true — to correct for mistakes in the search query (e.g., if you enter Beetles, the system will search for Beatles) 119 | * @param bool $lyrics - true — to return only audio files that have associated lyrics 120 | * @param bool $performer_only - true — to search only by artist name 121 | * @param int $sort - Sort order: 122 | * 1 — by duration 123 | * 2 — by popularity 124 | * 0 — by date added 125 | * @param bool $search_own - 126 | * @param int $offset - Offset needed to return a specific subset of audio files. (default: 0) 127 | * @param int $count - Number of audio files to return. (default: 30, max: 300) 128 | * @throws VkException 129 | * @return array 130 | */ 131 | public function search($q, $auto_complete, $lyrics, $performer_only, $sort, $search_own, $offset = 0, $count = 30){ 132 | $r = array( 133 | "q" => $q, 134 | "auto_complete" => $auto_complete, 135 | "lyrics" => $lyrics, 136 | "performer_only" => $performer_only, 137 | "sort" => $sort, 138 | "search_own" => $search_own, 139 | "offset" => $offset, 140 | "count" => $count 141 | ); 142 | return $this->api("audio.search",$r); 143 | } 144 | 145 | /** 146 | * @throws VkException 147 | * @return string 148 | */ 149 | public function getUploadServer(){ 150 | $r = $this->api("audio.search"); 151 | //TODO: Exception 152 | return $r['upload_url']; 153 | } 154 | 155 | //TODO 156 | /** https://vk.com/dev/audio.save 157 | * 158 | */ 159 | public function save(){} 160 | 161 | /** 162 | * @param int $audio_id - Audio file ID. 163 | * @param int $owner_id - ID of the user or community that owns the audio file. 164 | * @param int $group_id - Community ID, needed when adding the audio file to a community. 165 | * @param int $album_id 166 | * @throws VkException 167 | * @return array 168 | */ 169 | public function addAudio($audio_id, $owner_id, $group_id = null, $album_id = null){ 170 | $r = array("audio_id" => $audio_id, "owner_id" => $owner_id); 171 | if(!is_null($group_id)) $r['group_id'] = $group_id; 172 | if(!is_null($album_id)) $r['album_id'] = $album_id; 173 | return $this->api("audio.add",$r); 174 | } 175 | 176 | /** 177 | * @param int $audio_id - Audio file ID. 178 | * @param int $owner_id - ID of the user or community that owns the audio file. 179 | * @throws VkException 180 | * @return array 181 | */ 182 | public function deleteAudio($audio_id, $owner_id){ 183 | return $this->api("audio.delete",array("audio_id" => $audio_id,"owner_id" => $owner_id)); 184 | } 185 | 186 | /** 187 | * @param int $owner_id - ID of the user or community that owns the audio file. 188 | * @param int $audio_id - Audio file ID. 189 | * @param string $artist - Name of the artist. 190 | * @param string $title - Title of the audio file. 191 | * @param string $text - Text of the lyrics of the audio file. 192 | * @param int/string $genre_name - Genre of the audio file. See the list of audio genres. 193 | * @param bool $no_search - true — audio file will not be available for search 194 | * false — audio file will be available for search (default) 195 | * @throws VkException 196 | * @return array 197 | */ 198 | public function editAudio($owner_id, $audio_id, $artist = null, $title = null, $text = null, $genre_id = null, $no_search = false){ 199 | $r = array("owner_id" => $owner_id,"audio_id" => $audio_id); 200 | if(!is_null($artist)) $r['artist'] = $artist; 201 | if(!is_null($title)) $r['title'] = $title; 202 | if(!is_null($text)) $r['text'] = $text; 203 | if($no_search){ $r['no_search'] = 1; }else{ $r['no_search'] = 0; } 204 | if(!is_null($genre_id)){ 205 | if(!is_numeric($genre_id)) $genre_id = $this->getIdGenreByName($genre_id); 206 | if($genre_id != false) $r['genre_id'] = $genre_id; 207 | } 208 | return $this->api("audio.edit",$r); 209 | } 210 | 211 | /** 212 | * @param int $audio_id - Audio file ID. 213 | * @param int $owner_id - ID of the user or community that owns the audio file. (current user id is used by default) 214 | * @param int $before - ID of the audio file before which to place the audio file. 215 | * @param int $after - ID of the audio file after which to place the audio file. 216 | * @throws VkException 217 | * @return array 218 | */ 219 | public function reorderAudio($audio_id, $owner_id = null, $before = null, $after = null){ 220 | $r = array("audio_id" => $audio_id); 221 | if(!is_null($owner_id)) $r['owner_id'] = $owner_id; 222 | if(!is_null($before)) $r['before'] = $before; 223 | if(!is_null($after)) $r['after'] = $after; 224 | return $this->api("audio.reorder",$r); 225 | } 226 | 227 | /** 228 | * @param int $audio_id - Audio file ID. 229 | * @param int $owner_id - ID of the user or community that owns the audio file. (current user id is used by default) 230 | * @throws VkException 231 | * @return array 232 | */ 233 | public function restoreAudio($audio_id, $owner_id = null){ 234 | $r = array("audio_id" => $audio_id); 235 | if(!is_null($owner_id)) $r['owner_id'] = $owner_id; 236 | return $this->api("audio.restore",$r); 237 | } 238 | 239 | //TODO 240 | /** https://vk.com/dev/audio.getAlbums 241 | * 242 | */ 243 | public function getAlbums(){ 244 | 245 | } 246 | 247 | 248 | //TODO 249 | /** https://vk.com/dev/audio.addAlbum 250 | * 251 | */ 252 | public function addAlbum(){ 253 | 254 | } 255 | 256 | 257 | //TODO 258 | /** https://vk.com/dev/audio.editAlbum 259 | * 260 | */ 261 | public function editAlbum(){ 262 | 263 | } 264 | 265 | //TODO 266 | /** https://vk.com/dev/audio.deleteAlbum 267 | * 268 | */ 269 | public function deleteAlbum(){ 270 | 271 | } 272 | 273 | //TODO 274 | /** https://vk.com/dev/audio.moveToAlbum 275 | * 276 | */ 277 | public function moveToAlbum(){ 278 | 279 | } 280 | 281 | 282 | //TODO 283 | /** https://vk.com/dev/audio.setBroadcast 284 | * 285 | */ 286 | public function setBroadcast(){ 287 | 288 | } 289 | 290 | 291 | //TODO 292 | /** https://vk.com/dev/audio.getBroadcastList 293 | * 294 | */ 295 | public function getBroadcastList(){ 296 | 297 | } 298 | 299 | 300 | //TODO 301 | /** https://vk.com/dev/audio.getRecommendations 302 | * 303 | */ 304 | public function getRecommendations(){ 305 | 306 | } 307 | 308 | 309 | //TODO 310 | /** https://vk.com/dev/audio.getPopular 311 | * 312 | */ 313 | public function getPopular(){ 314 | 315 | } 316 | 317 | 318 | /** https://vk.com/dev/audio.getCount 319 | * @param int $owner_id 320 | * @return array 321 | * @throws VkException 322 | */ 323 | public function getCount($owner_id){ 324 | return $this->api("audio.getCount",["owner_id" => $owner_id]); 325 | } 326 | 327 | } -------------------------------------------------------------------------------- /src/VkBot.php: -------------------------------------------------------------------------------- 1 | setAccessToken($c->getToken()); 35 | $this->setAppId($c->getAppId()); 36 | $this->setScopes($c->getScopes()); 37 | $this->init_classes(); 38 | } 39 | 40 | private function init_classes(){ 41 | require_once(__DIR__ . "/VkAccount.php"); 42 | $this->account = new VkAccount($this->getAccessToken(),$this); 43 | require_once(__DIR__ . "/VkApps.php"); 44 | $this->apps = new VkApps($this->getAccessToken(),$this); 45 | 46 | if(!is_null($this->app_id)){ 47 | $data = $this->apps->get($this->app_id); 48 | $this->is_standalone = ($data["items"][0]["type"]=="standalone"); 49 | } 50 | 51 | if($this->account->audio){ 52 | require_once(__DIR__ . "/VkAudios.php"); 53 | $this->audios = new VkAudios(); 54 | } 55 | if($this->account->messages){ 56 | require_once(__DIR__ . "/VkMessages.php"); 57 | $this->messages = new VkMessages(); 58 | } 59 | if($this->account->wall){ 60 | require_once(__DIR__ . "/VkWall.php"); 61 | $this->wall = new VkWall(); 62 | } 63 | //TODO: More Classes 64 | } 65 | 66 | public function start(){} //TODO 67 | 68 | /** 69 | * @return null|string 70 | */ 71 | public function getAccessToken() : ?string { 72 | return $this->access_token; 73 | } 74 | 75 | /** 76 | * @param null|string $access_token 77 | */ 78 | public function setAccessToken($access_token){ 79 | $this->access_token = $access_token; 80 | } 81 | 82 | /** 83 | * @return array 84 | */ 85 | public function getScopes() : array{ 86 | return $this->scopes; 87 | } 88 | 89 | /** 90 | * @param array $scopes 91 | */ 92 | public function setScopes($scopes){ 93 | $this->scopes = $scopes; 94 | } 95 | 96 | /** 97 | * @param string $method - Do api, http://vk.com/dev/methods 98 | * @param array $vars 99 | * @return array 100 | */ 101 | public function api($method = '', array $vars = []) : array { 102 | 103 | $vars['v'] = self::API_VERSION; 104 | $vars['access_token'] = $this->access_token; 105 | 106 | $params = http_build_query($vars); 107 | 108 | $url = $this->http_build_query($method, $params); 109 | 110 | return (array)$this->call($url); 111 | } 112 | 113 | /** 114 | * @param $method 115 | * @param string $params 116 | * @return string 117 | */ 118 | private function http_build_query($method, $params = '') : string{ 119 | return self::METHOD_URL . $method . '?' . $params; 120 | } 121 | 122 | /** 123 | * @param string $url 124 | * @return bool|mixed|string 125 | * @throws VkException 126 | */ 127 | private function call($url = ''){ 128 | if(function_exists('curl_init')) $json = $this->curl_post($url); else $json = file_get_contents($url); 129 | 130 | $json = json_decode($json, true); 131 | 132 | // Ошибки, которые могут возникнуть описаны тут: https://vk.com/dev/errors 133 | if(isset($json['error'])) { 134 | if ($json['error']['error_msg'] != "Captcha needed") { 135 | if (isset($json['error'], $json['error']['error_msg'], $json['error']['error_code'])) { 136 | throw new VkException($json['error']['error_msg'], $json['error']['error_code']); 137 | } 138 | } else { 139 | echo "Error detected! Need captcha! Captcha img: " . $json['error']['captcha_img']; 140 | } 141 | } 142 | 143 | if(isset($json['response'])) return $json['response']; 144 | 145 | return $json; 146 | } 147 | 148 | private function curl_post($url){ 149 | 150 | if(!function_exists('curl_init')) return false; 151 | 152 | $param = parse_url($url); 153 | 154 | if( $curl = curl_init() ) { 155 | 156 | curl_setopt($curl, CURLOPT_URL, $param['scheme'].'://'.$param['host'].$param['path']); 157 | curl_setopt($curl, CURLOPT_RETURNTRANSFER,true); 158 | curl_setopt($curl, CURLOPT_POST, true); 159 | curl_setopt($curl, CURLOPT_POSTFIELDS, $param['query']); 160 | curl_setopt($curl, CURLOPT_TIMEOUT, $this->timeout); 161 | $out = curl_exec($curl); 162 | 163 | curl_close($curl); 164 | 165 | return $out; 166 | } 167 | 168 | return false; 169 | } 170 | 171 | /** 172 | * @return VkAudios 173 | */ 174 | public function getAudiosApi(){ 175 | return $this->audios; 176 | } 177 | 178 | /** 179 | * @return VkMessages 180 | */ 181 | public function getMessagesApi(){ 182 | return $this->messages; 183 | } 184 | 185 | /** 186 | * @return VkWall 187 | */ 188 | public function getWallApi(){ 189 | return $this->wall; 190 | } 191 | 192 | /** 193 | * @return mixed 194 | */ 195 | public function getAppId() : ?int{ 196 | return $this->app_id; 197 | } 198 | 199 | /** 200 | * @param mixed $app_id 201 | */ 202 | public function setAppId($app_id){ 203 | $this->app_id = $app_id; 204 | if(!is_null($this->app_id)){ 205 | $data = $this->apps->get($this->app_id); 206 | $this->is_standalone = ($data["items"][0]["type"]=="standalone"); 207 | } 208 | } 209 | 210 | /** 211 | * @return mixed 212 | */ 213 | public function getTimeout(){ 214 | return $this->timeout; 215 | } 216 | 217 | /** 218 | * @param mixed $timeout 219 | */ 220 | public function setTimeout($timeout){ 221 | $this->timeout = $timeout; 222 | } 223 | 224 | /** 225 | * @return mixed 226 | */ 227 | public function getConnectTimeout(){ 228 | return $this->connect_timeout; 229 | } 230 | 231 | /** 232 | * @param mixed $connect_timeout 233 | */ 234 | public function setConnectTimeout($connect_timeout){ 235 | $this->connect_timeout = $connect_timeout; 236 | } 237 | 238 | 239 | } -------------------------------------------------------------------------------- /src/VkException.php: -------------------------------------------------------------------------------- 1 | getMessagesByDialogID($id);} 15 | 16 | //TODO documentation 17 | /** 18 | * @param $text 19 | * @param null $user_id 20 | * @param null $domain_name 21 | */ 22 | public function sendMessageToUser($text,$user_id = null, $domain_name = null){ 23 | $r = []; 24 | if (!is_null($domain_name)) $r["domain"] = $domain_name; 25 | if (!is_null($user_id)) $r["user_id"] = $user_id; 26 | $r["message"] = $text; 27 | $this->api('messages.send',$r); 28 | } 29 | 30 | /** 31 | * @param $chat_id 32 | * @param int $count 33 | * @return array|null 34 | */ 35 | public function getHistoryMessagesFromChat($chat_id,$count = 20){ 36 | $result = $this->api('messages.getHistory', array('chat_id' => $chat_id,"count" => $count,"start_message_id" => -$count)); 37 | if(isset($result["items"])) { 38 | return $result["items"]; 39 | }else{ 40 | return null; 41 | } 42 | } 43 | 44 | /** 45 | * @param int $out 46 | * @param null $offset 47 | * @param int $count 48 | * @param int $time_offset 49 | * @param int $filters 50 | * @param int $preview_length 51 | * @return array|null 52 | */ 53 | public function get($out = 0,$offset = null,$count = 20,$time_offset = 0,$filters = 0,$preview_length = 0){ 54 | $r = [ 55 | "out" => $out, 56 | "count" => $count, 57 | "time_offset" => $time_offset, 58 | "filters" => $filters, 59 | "preview_length" => $preview_length 60 | ]; 61 | if(!is_null($offset)) $r["offset"] = $offset; 62 | $result = $this->api('messages.get',$r); 63 | if(isset($result["items"])){ 64 | return $result["items"]; 65 | }else{ 66 | return null; 67 | } 68 | } 69 | 70 | } -------------------------------------------------------------------------------- /src/VkWall.php: -------------------------------------------------------------------------------- 1 | api("wall.search",["owner_id" => $owner_id,"count" => $count,"query" => $query]); 17 | return $wall["items"]; 18 | } 19 | 20 | } --------------------------------------------------------------------------------