Php保利威点播示例

前言:

参考地址

image-20210329094209871

代码:

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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/**
* User: 謝雲虎
* DateTime: 2021/12/13 14:35
* Desc:post请求curl
* @param string $url
* @param array $post_data
*/
public function post_curl($url = '',$post_data = array()){
$ch = curl_init();
$timeout = 360;
curl_setopt( $ch, CURLOPT_URL, $url);
curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt( $ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt( $ch, CURLOPT_POSTFIELDS, $post_data);
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
$output = curl_exec( $ch );
curl_close( $ch );
print_r($output);
}

/**
* User: 謝雲虎
* DateTime: 2021/12/13 14:36
* Desc:get请求curl
* @param string $url
*/
public function get_curl($url = ''){
$ch = curl_init();//初始化cURL
curl_setopt($ch,CURLOPT_URL,$url);//抓取指定网页
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);//要求结果为字符串并输出到屏幕上
curl_setopt($ch, CURLOPT_HEADER, 0);//设置header
$output = curl_exec($ch);//执行并获得HTML内容
curl_close($ch);//释放cURL句柄
print_r($output);
}
/**
* User: 謝雲虎
* DateTime: 2021/12/13 12:50
* Desc:返回当前的毫秒时间戳
* @return float
*/
public function msectime() {
list($msec, $sec) = explode(' ', microtime());
$msectime = (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
return $msectime;
}

/**
* User: 謝雲虎
* DateTime: 2021/12/13 19:11
* Desc:生成SHA1签名,保利威获取加密等信息
* @param array $params
* @return string
*/
public function get_sign_str($params = array()){
if(request()->isPost()) {
//按照参数名字典顺序排列,采用HTTP GET请求参数连接方式(key=value&key=value&key=value)拼接参数名与参数值
$str = $this->get_url_str($params);
//在字符串尾部加上secretkey
$config = config('common.polyv');
$str .= $config['secretkey'];
//然后计算SHA1,将SHA1结果转为大写字母,作为sign;
return strtoupper(sha1($str));
}
}

/**
* User: 謝雲虎
* DateTime: 2021/12/13 16:21
* Desc:处理url参数
* @param $params
* @return bool|string
*/
public function get_url_str($params){
ksort($params);
$str = '';
foreach ($params as $k => $val) {
$str .= "{$k}={$val}&";
}
$str = substr($str,0,strlen($str)-1);
return $str;
}

/**
* User: 謝雲虎
* DateTime: 2021/12/13 19:36
* Desc:查询视频信息
* @param $param
*/
public function get_video_time($param){
$config = config('common.polyv');
$params['vid'] = $param;
$params['ptime'] = $this->msectime();
$params['userId'] = $config['userid'];
$params['sign'] = $this->get_sign_str($params);
$url = 'http://api.polyv.net/v2/video/'.$config['userid'].'/get-video-info';
return $this->post_curl($url,$params);
}


/**
* User: 謝雲虎
* DateTime: 2021/12/13 19:36
* Desc:批量查询视频时长和大小
*/
public function get_video_info(){
if(request()->isPost()) {
$config = config('common.polyv');
$params['vids'] = '保利威存储中的vids';
$params['ptime'] = $this->msectime();
$params['userid'] = $config['userid'];
$params['sign'] = $this->get_sign_str($params);
$url = 'http://api.polyv.net/v2/video/'.$config['userid'].'/getSizeByCata?';
$url .= $this->get_url_str($params);
$this->get_curl($url);
}
}