Video.php
2.48 KB
1
2
3
4
5
6
7
8
9
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<?php
/**
* Created by PhpStorm.
* User: Hanson
* Date: 2017/1/13
* Time: 22:08
*/
namespace Hanson\Vbot\Message\Entity;
use Hanson\Vbot\Message\MediaInterface;
use Hanson\Vbot\Message\MediaTrait;
use Hanson\Vbot\Message\MessageInterface;
use Hanson\Vbot\Message\UploadAble;
use Hanson\Vbot\Support\Console;
use Hanson\Vbot\Support\FileManager;
class Video extends Message implements MessageInterface, MediaInterface
{
use UploadAble, MediaTrait;
static $folder = 'mp4';
public function __construct($msg)
{
parent::__construct($msg);
$this->make();
}
public static function send($username, $file)
{
$response = static::uploadMedia($username, $file);
if (!$response) {
Console::log("视频 {$file} 上传失败");
return false;
}
$mediaId = $response['MediaId'];
$url = sprintf(server()->baseUri . '/webwxsendvideomsg?fun=async&f=json&pass_ticket=%s', server()->passTicket);
$data = [
'BaseRequest' => server()->baseRequest,
'Msg' => [
'Type' => 43,
'MediaId' => $mediaId,
'FromUserName' => myself()->username,
'ToUserName' => $username,
'LocalID' => time() * 1e4,
'ClientMsgId' => time() * 1e4
]
];
$result = http()->json($url, $data, true);
if ($result['BaseResponse']['Ret'] != 0) {
Console::log('发送视频失败');
return false;
}
return true;
}
/**
* 根据MsgID发送文件
*
* @param $username
* @param $msgId
* @return mixed
*/
public static function sendByMsgId($username, $msgId)
{
$path = static::getPath(static::$folder);
static::send($username, $path . "/{$msgId}.mp4");
}
/**
* 下载文件
*
* @return mixed
*/
public function download()
{
$url = server()->baseUri . sprintf('/webwxgetvideo?msgid=%s&skey=%s', $this->msg['MsgId'], server()->skey);
$content = http()->request($url, 'get', [
'headers' => [
'Range' => 'bytes=0-'
]
]);
if(strlen($content) === 0){
Console::log('下载视频失败', Console::ERROR);
}else{
FileManager::download($this->msg['MsgId'] . '.mp4', $content, static::$folder);
}
}
public function make()
{
$this->download();
}
}