I study API VK, set the task to implement the upload of photos to the VK group album, in which there are rights to upload photos.

Getting a token, requesting groups, albums with the ability to upload images is a process passed. Now stuck directly on the download itself. I get an error when sending a request

100: it is invalid.

It is understandable, because the server after loading the photo returns something like:

[server] => 637125 [photos_list] => [] [aid] => 123456 [hash] => 4fc82033a7c5c8eed3021ff289775fec [gid] => 123456

I load in the following way:

public static function uploadImages($files, $group_id, $album_id) { //ΠŸΠΎΠ»ΡƒΡ‡Π°Π΅ΠΌ сСрвСр для Π·Π°Π³Ρ€ΡƒΠ·ΠΊΠΈ изобраТСния $params = array( 'group_id' => $group_id, 'album_id' => $album_id ); $api_res = self::queryApi("photos.getUploadServer", $params); try { if($api_res) { $server = $api_res->upload_url; $post = array(); for ($i=0; $i < count($files); $i++) { $query = self::$connection->query('SELECT * FROM `images` WHERE `id` = '.$files[$i]); while($row = $query->fetch_assoc()) { if(!empty($row['image'])){ if(file_exists(config::$full_path.'images/'.$row['image'])){ $img = $row['image']; }else{ $img = 'no-image.jpg'; } }else{ $img = 'no-image.jpg'; } $post[] = array('file'.$i+1 => '@'.config::$full_path.'images/'.$img); } } if($ch = curl_init($server)) { curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POSTFIELDS,$post); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: multipart/form-data; charset=UTF-8')); $json = json_decode(curl_exec($ch)); curl_close($ch); $params = array( 'server' => $json->server, 'photos_list' => stripslashes($json->photos_list), 'album_id' => $album_id, 'hash' => $json->hash, 'gid' => $group_id ); $photo_save = self::queryApi('photos.save', $params); ////////////// print_r($json); ////////////// if($photo_save) { return $photo_save; } else throw new Exception("ΠŸΡ€ΠΈ сохранСнии Ρ„ΠΎΡ‚ΠΎΠ³Ρ€Π°Ρ„ΠΈΠΉ Π² альбом ΠΏΡ€ΠΎΠΈΠ·ΠΎΡˆΠ»Π° ошибка.", 1); } else throw new Exception("Ошибка ΠΈΠ½ΠΈΡ†ΠΈΠ°Π»ΠΈΠ·Π°Ρ†ΠΈΠΈ cURL", 1); } else throw new Exception("ΠŸΡ€ΠΈ ΠΎΠ±Ρ€Π°Ρ‰Π΅Π½ΠΈΠΈ ΠΊ API photos.getUploadServer ΠΏΡ€ΠΎΠΈΠ·ΠΎΡˆΠ»Π° ошибка.", 1); }catch(Exception $e){ echo $e->getMessage(); return false; } } 

At the entrance, the ID of the files in the database, group and album. An array fileN => @FILE_PATH is formed and sent to the received server address.

The API request looks like this. obazom:

 public static function queryApi($api, $params = null, $type = 0) { if($type == 0) { $server = 'https://api.vk.com/method/'; }else{ $server = 'https://oauth.vk.com/'; } $params['access_token'] = $_SESSION['vk']['access_token']; $params['v'] = config::$vk_api['ver']; $postparams = http_build_query($params); try{ if($curl = curl_init()) { curl_setopt($curl, CURLOPT_URL, $server.$api); curl_setopt($curl, CURLOPT_RETURNTRANSFER,true); curl_setopt($curl, CURLOPT_POST, true); curl_setopt($curl, CURLOPT_POSTFIELDS, $postparams); $result = json_decode(curl_exec($curl)); curl_close($curl); if(isset($result->error)) { if($type == 0) { $error_msg = "ΠŸΡ€ΠΈ запросС ΠΊ VK API Π²ΠΎΠ·Π½ΠΈΠΊΠ»Π° ошибка. ERROR CODE: ".$result->error->error_code."; MESSAGE: ".$result->error->error_msg; }else{ $error_msg = "ΠŸΡ€ΠΈ ΠΏΠΎΠΏΡ‹Ρ‚ΠΊΠ΅ Π°Π²Ρ‚ΠΎΡ€ΠΈΠ·Π°Ρ†ΠΈΠΈ ΠΏΡ€ΠΎΠΈΠ·ΠΎΡˆΠ»Π° ошибка. ERROR: ".$result->error."; DESCRIPTION: ".$result->error_description; } throw new Exception($error_msg, 1); } else { return $result; } } else { throw new Exception("Ошибка ΠΈΠ½ΠΈΡ†ΠΈΠ°Π»ΠΈΠ·Π°Ρ†ΠΈΠΈ модуля cURL.", 1); } }catch(Exception $e){ echo $e->getMessage(); return false; } } 

But as a result of the work I get an empty photos_list

The variable $ full_path = 'D: / OpenServer / domains / localhost /';

I ask to help to understand and send in the necessary manual) Thank you.

    1 answer 1

    The question was decided by replacing the first function of the line:

      $post[] = array('file'.$i+1 => '@'.config::$full_path.'images/'.$img); 

    on

      $post['file'.$i+1] = new CURLFile(config::$full_path.'images/'.$img); 

    Documentation HERE

    Works with PHP 5.6+