Skip to content

Instantly share code, notes, and snippets.

@nccdr
Last active September 18, 2016 19:28
Show Gist options
  • Save nccdr/363f938ebc6831c58ad8cd559dfe405c to your computer and use it in GitHub Desktop.
Save nccdr/363f938ebc6831c58ad8cd559dfe405c to your computer and use it in GitHub Desktop.
Отправка фейкового стикера в сообщения ВКонтакте
<?php
class VK
{
private $token;
private $version;
private $url = "https://api.vk.com/method/";
public function __construct($token, $version = "5.54")
{
$this->token = $token;
$this->version = $version;
}
private function uploadPhoto($server, $path)
{
$file = new CURLFile($path);
$data = [
"file"=>$file,
"title"=>"nicecoder"
];
$result = $this->post($server, $data);
return json_decode($result)->file;
}
private function uploadSticker($path)
{
$uploadServer = $this->api("docs.getUploadServer", array(
"v"=>$this->version,
"type"=>"graffiti"
))->response->upload_url;
$photoString = $this->uploadPhoto($uploadServer, $path);
$saveSticker = $this->api("docs.save", ["file"=>$photoString]);
$owner_id = $saveSticker->response[0]->owner_id;
$sticker_id = $saveSticker->response[0]->did;
return "doc".$owner_id."_".$sticker_id;
}
public function sendFakeStickerToUser($to, $path)
{
$attachmentId = $this->uploadSticker($path);
$sendMessage = $this->api("messages.send", [
"v"=>$this->version,
"peer_id"=>$to,
"attachment"=>$attachmentId
]);
return $sendMessage;
}
public function sendFakeStickerToChat($to, $path)
{
$attachmentId = $this->uploadSticker($path);
$sendMessage = $this->api("messages.send", [
"v"=>$this->version,
"chat_id"=>$to,
"attachment"=>$attachmentId
]);
return $sendMessage;
}
private function post($url, $data)
{
$ch = curl_init( $url );
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$response = curl_exec( $ch );
curl_close( $ch );
return $response;
}
public function api($method, $params = array())
{
$url = $this->url . $method;
if(is_array($params)){
$params["access_token"] = $this->token;
}else{
$params .= "&access_token=".$this->token;
}
$response = $this->post($url, $params);
$response = json_decode($response);
return $response;
}
}
$token = "Токен с доступом к сообщениям";
$vk = new VK($token);
# отправка стикера пользователю
$sendToUser = $vk->sendFakeStickerToUser("id пользователя", "Desktop/sticker.png");
# отправка стикера в чат
$sendToChat = $vk->sendFakeStickerToChat("id чата", "Desktop/sticker.png");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment