R
R
rena-nip2016-08-14 18:43:26
PHP
rena-nip, 2016-08-14 18:43:26

VK api, photos are not uploaded to the server?

//декодирую адрес картинки 
$image_url = urlencode($image_url);


$response = 'https://api.vkontakte.ru/method/photos.getWallUploadServer?owner_id='.$group_id.'&access_token='.$access_token;

//получаю URL для загрузки на сервер
$response = @file_get_contents($response); 
$response = json_decode($response);
$upload_url = $response->response->upload_url;

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $upload_url); 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, array('file1' => $image_url)); 
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: multipart/form-data; charset=UTF-8'));
$otvet = curl_exec($ch); 
curl_close($ch);
$otvet = json_decode($otvet);
print_r($otvet);

I get this response:
[server] => 630317 [photo] => [] [hash] => 78730dd19f6e53a70b7f54fbb7803519

Where is the mistake?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
Petr Flaks, 2016-08-14
@neluzhin

For some reason, you are transferring the encoded address of the image to VK. But you must transfer the file itself. You must upload the image to the server and then rewrite the line
so if you have PHP less than 5.5:
or if PHP is greater than or equal to 5.5:
This line can be removed:
Also, use the api.vk.com domain to access the API and don't forget to pass the API version in the v parameter in your requests .
UPD: also, as noted earlier, to upload photos to the wall, you must pass photos in the photo field , not file{...}.

H
hoarywolf, 2016-08-14
@hoarywolf

I think the error is in photo1, like the api for the wall just wants photo, and photo{1-5} only for uploading to the album.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question