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
120


/**
* 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
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$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['app_secret'];
//然后计算MD5,将MD5结果转为大写字母,作为sign;
return strtoupper(MD5($str));
}
}

/**
* User: 謝雲虎
* DateTime: 2021/12/13 16:21
* Desc:处理url参数
* @param $params
* @return bool|string
*/
public function get_url_str($params){
ksort($params);
$config = config('common.polyv');
$str = $config['app_secret'];
foreach ($params as $k => $val) {
$str .= "{$k}{$val}";
}
return $str;
}

/**
* User: 謝雲虎
* DateTime: 2022/4/1 15:36
* Desc:查询账号信息
*/
public function get_user_info(){
if(request()->isPost()) {
$config = config('common.polyv');
$params['timestamp'] = $this->msectime();
$params['appId'] = $config['app_id'];
$params['sign'] = $this->get_sign_str($params);
$url = 'https://api.polyv.net/live/v3/user/get-info?appId='.$params['appId'].'&sign='.$params['sign'].'&timestamp='.$params['timestamp'];
$this->get_curl($url);
}
}

/**
* User: 謝雲虎
* DateTime: 2022/4/1 15:36
* Desc:查询账号可用直播分钟数
*/
public function get_time(){
if(request()->isPost()) {
$config = config('common.polyv');
$params['timestamp'] = $this->msectime();
$params['appId'] = $config['app_id'];
$params['sign'] = $this->get_sign_str($params);
$url = 'http://api.polyv.net/live/v2/user/get-user-durations';
$this->post_curl($url,$params);
}
}