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.