- 01
- 02
- 03
- 04
- 05
- 06
- 07
- 08
- 09
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
function send_message_with_photo($token, $peer_id, $message, $image_file_path) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
/* Получаем ссылку для загрузки фотографии */
curl_setopt($ch, CURLOPT_URL, 'https://api.vk.com/method/photos.getMessagesUploadServer');
curl_setopt($ch, CURLOPT_POSTFIELDS, array(
"access_token" => $token,
"v" => "5.103"
));
$result = json_decode(curl_exec($ch), true);
$upload_url = $result['response']['upload_url'];
/* Отправляем фотографию */
curl_setopt($ch, CURLOPT_URL, $upload_url);
$file = curl_file_create($image_file_path);
curl_setopt($ch, CURLOPT_POSTFIELDS, array(
"photo" => $file,
));
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: multipart/form-data'));
$result = json_decode(curl_exec($ch), true);
$server = $result['server'];
$photo = $result['photo'];
$hash = $result['hash'];
/* Сохраняем фотографию */
curl_setopt($ch, CURLOPT_URL, 'https://api.vk.com/method/photos.saveMessagesPhoto');
curl_setopt($ch, CURLOPT_POSTFIELDS, array(
"access_token" => $token,
"v" => "5.103",
"server" => $server,
"photo" => $photo,
"hash" => $hash
));
$result = json_decode(curl_exec($ch), true);
$photo_id = strval($result['response'][0]['id']);
$owner_id = strval($result['response'][0]['owner_id']);
$attachment = "photo" . $owner_id . "_" . $photo_id;
/* Отправляем сообщение */
curl_setopt($ch, CURLOPT_URL, 'https://api.vk.com/method/messages.send');
curl_setopt($ch, CURLOPT_POSTFIELDS, array(
"access_token" => $token,
"v" => "5.103",
"random_id" => rand(),
"peer_id" => $peer_id,
"message" => $message,
"attachment" => $attachment
));
$result = json_decode(curl_exec($ch), true);
curl_close($ch);
}