A
A
Alexander2016-12-04 23:00:55
API
Alexander, 2016-12-04 23:00:55

Why is the VK API request not working?

I'm trying to write a function to add a product to the VK group, but I got stuck at the last stage - the actual addition itself ...
Actually, here's the function:

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' => $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['photo_604'];
  
  $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);
  
  print_r($addMarket);
}

Actually, the function consists of 3 parts:
1) We get a link to which we will upload a photo
2) We upload a photo
3) Add a product
Photos are loaded, but the product is not added, here is the error:
[error_code] => 100 [error_msg] => One of the parameters specified was missing or invalid: main_photo_id is undefined

It is clear that something is wrong with the main_photo_id, but what exactly?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrzej Wielski, 2016-12-04
@wielski

You are passing a link to a photo, but you need to pass the ID of the media record.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question