Answer the question
In order to leave comments, you need to log in
How to correctly upload images through the VKontakte API?
Hello. Please help me with the following issue. You need to upload a photo to the group's album via the VK API. I got to the point of getting the URL for uploading a photo, and then I do the following:
//Получили URL для загрузки
$uploadUrl = $result->response->upload_url;
//Путь до картинки. Тут у меня еще есть вопрос, а можно ли использовать пути типа http://example.com/test/image.jpg ? Хотя это не важно на данный момент
$file_path = "images/2.jpg";
$postParam = array("file1"=> "@".$file_path);
$ch = curl_init($uploadUrl);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postParam);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: multipart/form-data; charset=UTF-8'));
$json = json_decode(curl_exec($ch));
curl_close($ch);
stdClass Object
(
[server] => 630830
[photos_list] => []
[aid] => 234908050
[hash] => a14bbef18e6d15798955f9a4ba59a77e
[gid] => 126877620
)
Answer the question
In order to leave comments, you need to log in
With grief in half figured out what was the problem.
The point is that using curl_setopt ( $ch, CURLOPT_POSTFIELDS, array( 'file1' => '@' . $file ) );
since version 5.5. undesirable. On my server is 5.6.
Instead of this line, you should use
curl_setopt($ch, CURLOPT_POSTFIELDS, ['file1' => new CurlFile($file_path)]);
Good afternoon.
First, check out the following code.
$file_path = "images/2.jpg";
$postParam = array("file1"=> "@".$file_path);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: multipart/form-data; charset=UTF-8'));
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question