R
R
reklama_v_vk_20182018-06-21 01:32:45
API
reklama_v_vk_2018, 2018-06-21 01:32:45

How to upload a document via api vk?

Tell me how to upload a document (gif) to your account via api vk
. In the future, I want to attach the uploaded document to posts posted on the walls of different groups.

Here is my script, tell me what's wrong !!!!

<?php
$token = 'тут токен пользователя '; // токен с правами DOCS
$group = 142929375;
uploadDoc($group,$token);
// получение адреса сервера для загрузки документов
function uploadDoc($group,$token)
  {
    // 'group_id' => $group,  параметр убран из запроса , так как грузим в текущий аккаунт 
    $url = 'https://api.vk.com/method/docs.getUploadServer';
    $params = [
      'version' => 5.80,
      'access_token' => $token
    ];

    $getUrl = post($url, $params);
    if($getUrl)
    {
      	$getUrl = json_decode($getUrl, true);
      	$url = $getUrl['response']['upload_url']; // URL UPLOAD сервера
      	// загрузка файла на сервер ВК
      define('BASEPATH', str_replace('\\', '/', dirname(__FILE__)) . '/');
      $image = BASEPATH.'temp.gif';
      	$curl = curl_init();
      	curl_setopt($curl, CURLOPT_URL, $url);
      	curl_setopt($curl, CURLOPT_POST, true);
      	curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
     		curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); 
      	curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
      	curl_setopt($curl, CURLOPT_POSTFIELDS, ['photo' => new CURLFile($image, 'image/gif')], true);
      	$upload = curl_exec( $curl );
      	curl_close( $curl );
      	
      	// вывод принтов для наглядности происходящего
      	print_r('PATH = '.$image.'<br>');
      	print_r('SERVER = '.$url.'<br>');
      	print_r('UPLOAD_INFO(file) = '.$upload.'<br>');
      
        if($upload)
        {
            $upload = json_decode($upload, true);
            $file = $upload['file'];
            $error = $upload['error'];
        $url = 'https://api.vk.com/method/docs.save';
        $params = [
          'file' => $file,
          'title' => 'test',
          'tags' => 'test',
          'version' => 5.80,
          'access_token' => $token
        ];
     		$uploadfile = post($url, $params);
     		
            print_r('UPLOAD_DOC = '.$uploadfile.'<br>'); 
          }
          else{}
      }
    else{}	
  }
// отправка запроса в ВК	
function post($url, $params) {
        $ch = curl_init(); 
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params)); 
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $result = curl_exec($ch);
        if(!$result){
        	$result = curl_error($ch);
        }
        curl_close($ch);
        return $result;
  }
  ?>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Kudis, 2018-06-22
@kudis

I want some logs!
But guess:
Is the token obtained from the scope docs ?
maybe your php version is < 5.5, then new CURLFile() won't work
, I don't like this line:
Here it is indicated that the parameter should be called file

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question