M
M
Max Payne2015-01-20 14:47:28
PHP
Max Payne, 2015-01-20 14:47:28

Why is vk api returning empty "photo":"[]"?

I use the following code to upload photos to VK:

$is = 'https://api.vk.com/method/photos.getMessagesUploadServer?access_token='.$token;
$is = json_decode(file_get_contents($is),true);
$photo = array(
    "file1" => "/tmpimg.png",
);
$url = $is['response']['upload_url'];
$params = array(
    'photo' => $photo, 
);
$result = file_get_contents($url, false, stream_context_create(array(
    'http' => array(
        'method'  => 'POST',
        'header'  => 'Content-type: application/x-www-form-urlencoded',
        'content' => http_build_query($params)
    )
)));
echo $result;

Return result:
{"server":624128,"photo":"[]","hash":"?1492ef?5b64c?d648dd4???e?cc931a"}

Why is "photo" empty?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Maxim Vasilchenko, 2015-01-23
@YardalGedal

function file_get_contents_curl($url) {
   $ch = curl_init();
   
   curl_setopt ($ch,CURLOPT_USERAGENT,'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1');
   curl_setopt($ch, CURLOPT_HEADER, 0);
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //Set curl to return the data instead of printing it to the browser.
   curl_setopt($ch, CURLOPT_URL, $url);
   
   $data = curl_exec($ch);
   curl_close($ch);
   
   return $data;
}
// грузим фото вк
$upurl =  'https://api.vk.com/method/photos.getMessagesUploadServer?access_token='.$token;
  $upl = file_get_contents_curl($upurl); // Отправляем запрос
  $uplj = json_decode($upl); // Преобразуем JSON-строку в массив
  $VKuploadUrl = $uplj->response->upload_url;
if (!empty($VKuploadUrl)) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $VKuploadUrl);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, array('photo' => '@'.$SERVER['document_root'].$imggr));
 
$otvet = curl_exec($ch);
 
curl_close($ch);
 }

J
Jim_Di, 2015-06-19
@JimDi

a bit of necroposting, I hope it helps someone not to waste time.
instead of
should be something like this

$cfile = curl_file_create($fullServerPathToImage,'image/jpeg',pathinfo($fullServerPathToImage)['basename']);
        $data = array('file' => $cfile);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

well, you may need this piece, otherwise https is everywhere now
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question