R
R
rusgayfer2017-05-30 22:54:38
PHP
rusgayfer, 2017-05-30 22:54:38

Uploading photos to VK API album (empty photos_list value)?

Help what is wrong. Returns empty on save

if (isset($_POST['reload']) && $_POST['reload'] !== '') {
    if (isset($rowCount2) == isset($groupId[1])) {
    $allowFiles = ['jpg', 'png', 'gif', 'bmp', 'jpeg'];
    $attachments = '';  
    $account['token'] = 'тут токен';
    $account['album'] = 243088786;
    $token = $account['token'];
     $attachforpost = '';
   // загрузка обложки
    if (is_uploaded_file($_FILES['file']['tmp_name'])) {
        $ext = pathinfo($_FILES['file']['name']);
        if(!isset($ext['extension'])) { $ext['extension'] = '';}
        $ext = strtolower($ext['extension']);
            if (!in_array($ext, $allowFiles)) {
                $message = "<div class=\"callout callout-danger\">Ошибка при загрузке файла: загрузить можно только файлы с расширением jpg, png, gif, bmp, jpeg. </div>";} 
            elseif(!is_uploaded_file($_FILES["file"]["tmp_name"]) || !move_uploaded_file($_FILES["file"]["tmp_name"], "/tmp/{$token}_".$_FILES["file"]["name"])) {
                $message = "<div class=\"callout callout-danger\"> Ошибка при загрузке файла: попробуйте еще раз. </div>";$_SESSION['token'] = $token; } 
            else {
            $members = json_decode(file_get_contents("https://api.vk.com/method/photos.getUploadServer?album_id={$account['album']}&group_id=79668981&access_token={$account['token']}"),true);
           //var_dump($members);
                if (isset($members['response']['error'])) {
                $message = "<div class=\"callout callout-danger\"> Ошибка при загрузке файла: Сервис недоступен, попробуйте повторить позднее. </div>"; 
                $_SESSION['token'] = $token;
                } else {  
                    $curl = curl_init($members['response']['upload_url']);
                    $opts = [
                      CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows NT 6.1; rv:6.0.2) Gecko/20100101 Firefox/6.0.2',
                      CURLOPT_RETURNTRANSFER => true,
                      CURLOPT_SSL_VERIFYPEER => false,
                      CURLOPT_SSL_VERIFYHOST => false,
                      CURLOPT_POSTFIELDS => [
                        'file1' => "@/tmp/{$token}_".$_FILES["file"]["name"]
                      ]
                    ]; 
                    curl_setopt_array($curl, $opts);
                    $photoRequest = json_decode(curl_exec($curl), true); 
                    $membeer = json_decode(file_get_contents("https://api.vk.com/method/photos.save?server={$photoRequest['server']}&photos_list={$photoRequest['photos_list']}&album_id={$account['album']}&group_id=79668981&hash={$photoRequest['hash']}&access_token={$account['token']}"),true);
                   	//var_dump($membeer);	
                    if (isset($request['response']['error'])) {
                    $message = "<div class=\"callout callout-danger\"> Ошибка при загрузке файла: Сервис недоступен, попробуйте повторить позднее. </div>";
                    $_SESSION['token'] = $token; } 
                    else { $attachforpost = $membeer['response'][0]["src"];
                         }					 
                }
            }
    } 
}
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey Sokolov, 2017-05-30
@rusgayfer

Try instead

'file1' => "@/tmp/{$token}_".$_FILES["file"]["name"]

do this way:
The point is that "As of PHP 5.5.0, the @ prefix is ​​deprecated and files can be uploaded using CURLFile ."

M
MaxTheCapybara, 2017-05-30
@MaxTheCapybara

I saw a similar question. The structure of the request is well laid out there, and curl is also used
How to upload a photo to the VK server (c ++, curl)?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question