R
R
rusgayfer2018-01-31 01:47:33
PHP
rusgayfer, 2018-01-31 01:47:33

Why is there an empty request in the photo field?

Does not upload photos to the VK server, because the path is not correct

// Функция вызова API
  function by($method, $array = false) {
    $response = curl("https://api.vk.com/method/".$method, $array);
    $json = json_decode($response, true);
    if($json["error"]["error_msg"]) {
      return $json["error"];
    } else {
      return $json["response"];
    }
  }
  
// Функция сохранения картинки на сервер	
  function download($picture = null, $group = null) {
    global $token, $group_post;
    $pic = curl_init($picture);
    $file = fopen("image.jpg", "wb");
    curl_setopt($pic, CURLOPT_FILE, $file);
    curl_setopt($pic, CURLOPT_HEADER, 0);
    curl_exec($pic);
    curl_close($pic);
    $getWallUploadServer = by("photos.getWallUploadServer", array("group_id" => $group_post, "access_token" => $token));
    $upload = curl($getWallUploadServer["upload_url"], array("file1" => "@".dirname(__FILE__)."/image.jpg"));
    $json = json_decode($upload, true);
    $saveWallPhoto = by("photos.saveWallPhoto", array("group_id" => $group_post, "photo" => $json["photo"], "hash" => $json["hash"], "server" => $json["server"], "access_token" => $token));
    return $saveWallPhoto[0]["id"];
  }
  

  function curl($url, $post = false) {
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.4 (KHTML, like Gecko) Chrome/22.0.1229.94 Safari/537.4 AlexaToolbar/alxg-3.1');
    if ($post) { 
      curl_setopt($ch, CURLOPT_POST, 1);
      curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
    }
    $response = curl_exec($ch);
    curl_close($ch);
    return $response;
  }

For this code, the path works like this
/home/q/m95351vh/site.ru/public_html/script/tools/image.jpg

How to specify the path correctly if my image is located on this path
/home/q/m95351vh/site.ru/public_html/script/image.jpg

And why do I have an empty photo
print_r($saveWallPhoto);

(
    [error_code] => 100
    [error_msg] => One of the parameters specified was missing or invalid: photos_list is invalid
    [request_params] => Array
        (
            [0] => Array
                (
                    [key] => oauth
                    [value] => 1
                )

            [1] => Array
                (
                    [key] => method
                    [value] => photos.saveWallPhoto
                )

            [2] => Array
                (
                    [key] => group_id
                    [value] => 55555552
                )

            [3] => Array
                (
                    [key] => photo
                    [value] => []
                )

            [4] => Array
                (
                    [key] => hash
                    [value] => c5775a11191e58052774f89f3556775d
                )

            [5] => Array
                (
                    [key] => server
                    [value] => 740460
                )

        )

)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Ainur Valiev, 2018-01-31
@vaajnur

$file = fopen("image.jpg", "wb");
you will open if only the picture is in the same directory as the script itself. So it's better not to specify the path. Push off
Again, this way is also undesirable.
dirname(__FILE__)."/image.jpg"

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question