I
I
idccc2016-01-31 18:22:45
PHP
idccc, 2016-01-31 18:22:45

How to send a picture to VKontakte from the server using the Curl method?

Good afternoon.
I ran into a problem when uploading an image to the VKontakte server from a remote server using curl/php.
For uploading, the getWallUploadServer method is used, which returns a link to upload an image from the VK server. We need to make a post request to this link with the 'photo' field, which contains the image file.
I pass the parameters, but the response tells me that I am not sending pictures.

curl_setopt($ch, CURLOPT_URL, $url);  
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($ch, CURLOPT_VERBOSE , 1);
    $post_params['photo'] = $_SERVER['DOCUMENT_ROOT'].'/img/site/ban/dogma.jpg';
    $post_params['type'] = 'image/jpeg';
    curl_setopt($curl, CURLOPT_POSTFIELDS, $post_params);
    $output = curl_exec($ch);

curl_getinfo says the following
size_upload 0
size_download 72

Does this mean that the data was not transferred in the principle?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Eugene, 2016-01-31
@idccc

public function uploadFile($url, $path)
    {
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_POST, true);

        if (class_exists('\CURLFile')) {
            curl_setopt($ch, CURLOPT_POSTFIELDS, ['file1' => new \CURLFile($path)]);
        } else {
            curl_setopt($ch, CURLOPT_POSTFIELDS, ['file1' => "@$path"]);
        }

        $data = curl_exec($ch);
        curl_close($ch);
        return json_decode($data, true);
    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question