P
P
paramin2020-07-17 14:40:48
API
paramin, 2020-07-17 14:40:48

How to upload photos in vk api 2020?

Hello. I want to add some photos via api to the group wall.

Stuck on step 2, uploading photos to the VK server. Returns
{"server":856008,"photo":"[]","hash":"cac6cf0948f0d09619fed86bbfe2bed0"}"

I found a bunch of topics on Google, but I couldn't fix it myself. Please tell me what I did wrong.

//Получаем url загрузки
  $upload_url = json_decode(file_get_contents('https://api.vk.com/method/photos.getWallUploadServer?group_id='.$group_id.'&v='.$v.'&access_token='.$access_token))->response->upload_url;

  var_dump($upload_url);
  
  $images = array(
    "photo" => ['https://example.ru/photo/18270.jpg']
  );

    //Загружаем на сервер
  $upload = curl($upload_url, $images);
  
  var_dump($upload);
  
  
  function curl($url, $params){
    
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true );
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false );
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false );
    curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type:multipart/form-data"));
    curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
    $result = curl_exec( $ch );
    curl_close( $ch );
    
    return $result;
  }

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question