Answer the question
In order to leave comments, you need to log in
How to upload VK API PHP 7 file?
Initially, I tested the program on a local server with php version 5.5 installed. To upload the file I used the following function:
public function savePhoto($aid, $gid, $file, $caption){//Загрузка фото в группу
$response = $this->get("photos.getUploadServer", array(
"album_id" => $aid,
"group_id" => $gid
));
$ch = curl_init();
$data = array('file' => '@' . $file);
curl_setopt($ch, CURLOPT_URL, $response->response->upload_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$res = curl_exec($ch);
$res = json_decode($res);
$resp = $this->get("photos.save", array(
"album_id" => $aid,
"group_id" => $gid,
"server" => $res->server,
"photos_list" => $res->photos_list,
"hash" => $res->hash,
"caption" => $caption
));
return $resp;
}
$response = $this->get("photos.getUploadServer", array(
"album_id" => $aid,
"group_id" => $gid
));
$ch = curl_init();
$curlfile = new CURLFile($file, 'image/jpeg', 'kvartira.jpg');
$data = array("file" => $curlfile);
curl_setopt($ch, CURLOPT_URL, $response->response->upload_url);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
$res = curl_exec($ch);
echo curl_error($ch);
curl_close ($ch);
$res = json_decode($res);
$resp = $this->get("photos.save", array(
"album_id" => $aid,
"group_id" => $gid,
"server" => $res->server,
"photos_list" => $res->photos_list,
"hash" => $res->hash,
"caption" => $caption
));
object(stdClass)#7 (5) {
["server"]=>
int(631619)
["photos_list"]=>
string(2) "[]"
["aid"]=>
int(215173582)
["hash"]=>
string(32) "3d6f2fbe2d056681143a1800ab054d97"
["gid"]=>
int(93036481)
}
["error_code"]=>
int(100)
["error_msg"]=>
string(78) "One of the parameters specified was missing or invalid: photos_list is invalid"
$aPost = array(
'file' => new CURLFile($file)
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $response->response->upload_url);
curl_setopt($ch, CURLOPT_SAFE_UPLOAD, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $aPost);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$res = curl_exec ($ch);
curl_close($ch);
$res = json_decode($res);
Answer the question
In order to leave comments, you need to log in
$aPost = array(
'file' => new CURLFile($file)
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $response->response->upload_url);
curl_setopt($ch, CURLOPT_SAFE_UPLOAD, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $aPost);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$res = curl_exec ($ch);
curl_close($ch);
$res = json_decode($res);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question