I
I
Ilya Bychek2020-03-10 21:27:58
PHP
Ilya Bychek, 2020-03-10 21:27:58

How to send a photo on behalf of a bot?

Hello everyone, there is a code for uploading photos to the VK server:

$par = array(
    'access_token' => '///',
    'peer_id' => 424401660,
    'v' => 5.103
);
        
$user = json_decode(file_get_contents('https://api.vk.com/method/photos.getMessagesUploadServer?' . urldecode(http_build_query($par))), true);
$curl_file = curl_file_create("./photos/send/",'image/jpeg','1583856373.jpg');
$ch=curl_init();
curl_setopt_array($ch, 
  array(
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_URL => $user['response']['upload_url'],
    CURLOPT_POST => 1,
    CURLOPT_POSTFIELDS => array("photo" => $curl_file)
  ));

$img_attach = json_decode(curl_exec($ch), true);

print_r($img_attach);


But when the answer is displayed, it does not display anything! What could be the problem?
Thanks in advance

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
nokimaro, 2020-03-10
@nokimaro

You are using curl_file_create() incorrectly. The first parameter is to pass the path to the file.

curl_file_create ( string $filename [, string $mimetype [, string $postname ]] ) : CURLFile

https://www.php.net/manual/en/curlfile.construct.php

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question