R
R
Ruslan722014-05-14 12:50:02
PHP
Ruslan72, 2014-05-14 12:50:02

PHP, API vk.com - how to upload photos to the wall?

3 days trying to upload an image to VK.
I follow the manual https://vk.com/dev/upload_files
1. I get the url to upload the image
file_get_contents(' https://api.vk.com/method/photos.getWallUploadServ... '.$access_token);
2. I send a post to the received url
$img_src = '4.png';
$imgbinary = fread(fopen($img_src, "r"), filesize($img_src));
$img_str = base64_encode($imgbinary);
$post_params = array(
"file1" => $img_str,
);
file_get_contents(' https://api.vk.com/method/photos.getWallUploadServ... '.$access_token);
$dataArray=json_decode($data);
$ch = curl_init( $dataArray->
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, false );
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_params);
$response = curl_exec( $ch );
curl_close( $ch );
print_r($response);
An empty photo parameter comes in the response
example response
{"server":606321,"photo":"[]","hash":"afc01d1f75f443b30ff7b49254f642e1"}
Tell me what's wrong.
In vk technical support, they answered like this
. We only support downloading a file directly, and not at an address. Those. in file1 you must pass the byte array of the photo.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Kubintsev, 2014-05-14
@Ruslan72

1. The documentation says that the names of the POST fields are file1, file2,...,file5, and you have photo
2. An incorrect request for sending a file may be generated, try this:

$post_params = array(
    'file1' => '@'.$img_src,
);

Accordingly, the lines above
$imgbinary = fread(fopen($img_src, "r"), filesize($img_src));
$img_str = base64_encode($imgbinary);

you don't need

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question