K
K
Kevin Mitnick2016-05-26 20:21:45
PHP
Kevin Mitnick, 2016-05-26 20:21:45

How to upload VK API PHP 7 file?

Initially, I tested the program on a local server with php version 5.5 installed. To upload the file I used the following function:

public function savePhoto($aid, $gid, $file, $caption){//Загрузка фото в группу
    $response = $this->get("photos.getUploadServer", array(
      "album_id" => $aid,
      "group_id" => $gid
    ));

    $ch = curl_init();
    $data = array('file' => '@' . $file);
    curl_setopt($ch, CURLOPT_URL, $response->response->upload_url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    $res = curl_exec($ch);
    $res = json_decode($res);
    $resp = $this->get("photos.save", array(
      "album_id" => $aid,
      "group_id" => $gid,
      "server" => $res->server,
      "photos_list" => $res->photos_list,
      "hash" => $res->hash,
      "caption" => $caption
    ));

    return $resp;
  }

I transferred the program to a virtual server, php 7.0 is installed there, this method does not work there, as I understand it, this is due to the fact that in php above 5.5 it is necessary to transfer files via curl objects. Actually, now I use the following code:
$response = $this->get("photos.getUploadServer", array(
  "album_id" => $aid,
  "group_id" => $gid
));

$ch = curl_init();
$curlfile = new CURLFile($file, 'image/jpeg', 'kvartira.jpg');
$data = array("file" => $curlfile);
curl_setopt($ch, CURLOPT_URL, $response->response->upload_url);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
$res = curl_exec($ch);
echo curl_error($ch);
curl_close ($ch);

$res = json_decode($res);

$resp = $this->get("photos.save", array(
  "album_id" => $aid,
  "group_id" => $gid,
  "server" => $res->server,
  "photos_list" => $res->photos_list,
  "hash" => $res->hash,
  "caption" => $caption
));

And in response from the API, I always get first:
object(stdClass)#7 (5) {
  ["server"]=>
  int(631619)
  ["photos_list"]=>
  string(2) "[]"
  ["aid"]=>
  int(215173582)
  ["hash"]=>
  string(32) "3d6f2fbe2d056681143a1800ab054d97"
  ["gid"]=>
  int(93036481)
}

Then, when trying to save a photo:
["error_code"]=>
    int(100)
    ["error_msg"]=>
    string(78) "One of the parameters specified was missing or invalid: photos_list is invalid"

It is understandable, because "photos_list" is empty, the file is not sent in the request. Who knows how you can send a file request to a URL from a php script?
UPD:
After several hours of searching, I finally found the answer to my question. After php version above 5.5, you need to shaman with "CURLOPT_SAFE_UPLOAD" so that you can give files directly from the script. Actually, here is the working code:
$aPost = array(
    'file' => new CURLFile($file)
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $response->response->upload_url);
curl_setopt($ch, CURLOPT_SAFE_UPLOAD, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $aPost);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$res = curl_exec ($ch);
curl_close($ch);
$res = json_decode($res);

Answer the question

In order to leave comments, you need to log in

2 answer(s)
K
Kevin Mitnick, 2016-05-27
@volfing

$aPost = array(
    'file' => new CURLFile($file)
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $response->response->upload_url);
curl_setopt($ch, CURLOPT_SAFE_UPLOAD, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $aPost);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$res = curl_exec ($ch);
curl_close($ch);
$res = json_decode($res);

M
Michael, 2016-05-26
@springimport

Curl is too low, ie. low-level. I can advise you to switch to Guzzle .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question