D
D
DuckerMan2017-03-15 17:58:49
PHP
DuckerMan, 2017-03-15 17:58:49

How to upload a photo through VK api?

Greetings.
I decided to upload files to VK.
The code :

function DoPostRequestNigga($url, $post_data){


  
  $post_data = http_build_query($post_data); // Шифруем,для передачи на сервер


$ch = curl_init();  
  
curl_setopt($ch, CURLOPT_URL, $url);  
  
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  

  curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type:multipart/form-data"));
// указываем, что у нас POST запрос  
curl_setopt($ch, CURLOPT_POST, 1);  
curl_setopt($ch, CURLOPT_SAFE_UPLOAD, 1);  
// добавляем переменные  
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);  


  
$output = curl_exec($ch);  

echo __DIR__;
  return $output;
curl_close($ch);




  }



  print_r(DoPostRequestNigga($vk->request('photos.getMessagesUploadServer')['response']['upload_url'], array('photo' =>  __DIR__ . '/@photo.png')));

But, the photo in the response of the VK server is still empty.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Serezha, 2017-03-17
@DuckerMan

On 5.6 it works like this.
Example for loading into the dialog:
(important, see the documentation, the photo is loaded into the dialog in the photo field, in other places it can be loaded in the file field).

$photo = 'file.png';
    $postparam=array("photo"=>"@".$photo);
    $ch = curl_init($request_server->response->upload_url);
    curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS,$postparam);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: multipart/form-data; charset=UTF-8'));
    $upload = json_decode(curl_exec($ch));
    curl_close($ch);

For php7 using CURLFile and CURL without CURLOPT_SAFE_UPLOAD option
"photo"=> new CURLFile($photo)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question