M
M
MakStashkevich2016-12-06 17:18:39
PHP
MakStashkevich, 2016-12-06 17:18:39

How to upload and send photos in messages via VK API?

How to send upload photos in messages via VK API?
I do this:
PS Utils -> this is a separate class that is responsible for executing requests
1. I get the URL to upload my photo

//CODE
$server = Utils::requestMsg("photos.getMessagesUploadServer", ['access_token' => $token, 'user_id' => 123456789]);

//CODE Utils
public static function requestMsg($method, $params, $customURL = false) {
    $curl = curl_init(($customURL ? $customURL : 'https://api.vk.com/method/') . $method);
    $params['v'] = '5.59';
    
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($params));
    
    $answer = json_decode(curl_exec($curl), true);
    return $answer; 
                curl_close($curl);
  }

2. I send a request with a photo to the received address
//CODE
$img = "https://pp.vk.me/c9620/g28551727/d_93b73059.jpg";
$params = ['file1' => '@'.$img];
$main = Utils::requestPhoto($server['response']['upload_url'], $params);

//CODE Utils
public static function requestPhoto($url, $params, $customURL = false) {
  $curl = curl_init($url);

  curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true );
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false );
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false );
        curl_setopt($curl, CURLOPT_POSTFIELDS, $params);

        $resp = curl_exec($curl);
        return $resp;
        curl_close($curl);
}

As a result, I should get 3 parameters to upload a photo to the message, but I get only 2...
//An example of what I get
"{"server":604320,"photo":"[]","hash":"1019b9b1d22c958f4de7caed11380785"}"

What is the problem? Please help, I've been struggling with this for a long time...

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander, 2016-12-06
@SmoKE_xD

I will give an example of a working function for uploading photos for goods in VK, maybe it will help:

function VKaddGoodsOnMarket ($name,$description,$category_id,$price,$photourl) {
  $vkToken = '';
  $vkGroupID = '';
  $vkVersionAPI = '5.60';
  //Получаем ссылку для загрузки фото
  $GET_getMarketUploadServer = [
    'group_id' => $vkGroupID, 
    'main_photo' => 1,
    'access_token' => $vkToken,
    'v' => $vkVersionAPI
  ];
  $resukt_url_dp = json_decode(file_get_contents('https://api.vk.com/method/photos.getMarketUploadServer?'.http_build_query($GET_getMarketUploadServer)), TRUE);
  //Загружаем фото КУРЛом, отправляя ПОСТ на полученю ссылку
  $curl_file = curl_file_create($photourl,'image/jpeg','test_name.jpg');
  $ch=curl_init();
  curl_setopt_array($ch, array(
  CURLOPT_RETURNTRANSFER => 1,
  CURLOPT_URL => $resukt_url_dp['response']['upload_url'],
  CURLOPT_POST => 1,
  CURLOPT_POSTFIELDS => array("photo" => $curl_file)
  ));
  //Получим массив с хешем и прочим
  $img_attach = json_decode(curl_exec($ch), true);
  
  //Сохраняем фоточку отпрявляя ГЕТ запрос в ВК
  $GET_saveMarketPhoto = [
    'group_id' => $vkGroupID, 
    'photo' => stripslashes($img_attach['photo']),
    'server' => $img_attach['server'],
    'hash' => $img_attach['hash'],
    'crop_data' => $img_attach['crop_data'],
    'crop_hash' => $img_attach['crop_hash'],
    'access_token' => $vkToken,
    'v' => $vkVersionAPI
  ];
  $photo = json_decode(file_get_contents('https://api.vk.com/method/photos.saveMarketPhoto?'.http_build_query($GET_saveMarketPhoto)), TRUE);
  
  $photoGoods = $photo['response'][0]['id'];

  $GET_marketadd = [
    'owner_id' => '-'.$vkGroupID.'',
    'name' => $name,
    'description' => $description,
    'category_id' => $category_id,
    'price' => $price,
    'main_photo_id' => $photoGoods,
    'access_token' => $vkToken,
    'v' => $vkVersionAPI
  ];
  $addMarket = json_decode(file_get_contents('https://api.vk.com/method/market.add?'.http_build_query($GET_marketadd)), TRUE);
  
  return $addMarket;
}

A
Alexey, 2016-12-06
@AlSher101

For PHP 5.6+, file references ($params) should be passed in the following format:
Array('file1'=>new \CURLFile($img));

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question