189 lines
6.3 KiB
PHP
189 lines
6.3 KiB
PHP
<?php
|
|
/**
|
|
* Notes:保利威云直播
|
|
* Created on: 2022/2/8 15:03
|
|
* Created by: dengbw
|
|
*/
|
|
|
|
class Polyv
|
|
{
|
|
protected $url = "http://api.polyv.net/live";
|
|
protected $user_id = "549cf353c6";//2a3b848cd4
|
|
protected $app_id = "fr0myucldj";//g45uep0s6v
|
|
protected $app_secret = "4919b95df21e4afd80fe14ae8a722b1c";//447d5ce803d84f599794a3274177f796
|
|
protected $channel_id = "2005999";//2797712
|
|
protected $timestamp;//时间戳
|
|
|
|
public function __construct($param = [])
|
|
{
|
|
if ($param['app_id'] != '') $this->app_id = $param['app_id'];
|
|
if ($param['app_secret'] != '') $this->app_secret = $param['app_secret'];
|
|
if ($param['user_id'] != '') $this->user_id = $param['user_id'];
|
|
if ($param['channel_id'] != '') $this->channel_id = $param['channel_id'];
|
|
$this->timestamp = time() * 1000;
|
|
}
|
|
|
|
/**
|
|
* Notes:查询频道多场次概览统计数据
|
|
* https://help.polyv.net/index.html#/live/api/channel/viewdata/get_session_stats
|
|
* Created on: 2022/2/9 16:40
|
|
* Created by: dengbw
|
|
* @param array $params ['sessionIds' => 'g6uy09qeuq', 'startTime' => '1644310918000', 'endTime' => '1644318322000']
|
|
* @return bool|string
|
|
*/
|
|
function getSessionStats($params = [])
|
|
{
|
|
$params_sign = array(
|
|
'appId' => $this->app_id,
|
|
'timestamp' => $this->timestamp,
|
|
'userId' => $this->user_id,
|
|
'channelId' => $params['channelId'] ? $params['channelId'] : $this->channel_id,
|
|
);
|
|
$params['sessionIds'] && $params_sign['sessionIds'] = $params['sessionIds'];
|
|
$params['startTime'] && $params_sign['startTime'] = $params['startTime'];
|
|
$params['endTime'] && $params_sign['endTime'] = $params['endTime'];
|
|
$sign = $this->getSign($params_sign);
|
|
$parts = http_build_query($params_sign);
|
|
$url = $this->url . "/v3/channel/statistics/get-session-stats?{$parts}&sign={$sign}";
|
|
return $this->curlGet($url);
|
|
}
|
|
|
|
/**
|
|
* Notes:查询频道某段时间的直播观看详情数据
|
|
* https://help.polyv.net/index.html#/live/api/channel/viewdata/summary
|
|
* Created on: 2022/2/8 17:45
|
|
* Created by: dengbw
|
|
* @param array $params ['channelId' => '2797712', 'startDay' => '2022-01-01', 'endDay' => '2022-02-08']
|
|
* @return bool|string
|
|
*/
|
|
function summary($params = [])
|
|
{
|
|
$params_sign = array(
|
|
'appId' => $this->app_id,
|
|
'timestamp' => $this->timestamp,
|
|
'startDay' => $params['startDay'],
|
|
'endDay' => $params['endDay'],
|
|
);
|
|
$sign = $this->getSign($params_sign);
|
|
$parts = http_build_query($params_sign);
|
|
$url = $this->url . "/v2/statistics/{$params['channelId']}/summary?{$parts}&sign={$sign}";
|
|
return $this->curlGet($url);
|
|
}
|
|
|
|
/**
|
|
* Notes:查询频道直播观看详情数据
|
|
* https://help.polyv.net/index.html#/live/api/channel/viewdata/viewlog_2
|
|
* Created on: 2022/2/8 16:08
|
|
* Created by: dengbw
|
|
* @param array $params ['channelId' => '2797712', 'currentDay' => '2022-01-28']
|
|
* @return bool|string
|
|
*/
|
|
function viewLog($params = [])
|
|
{
|
|
$params_sign = array(
|
|
'appId' => $this->app_id,
|
|
'timestamp' => $this->timestamp,
|
|
'currentDay' => $params['currentDay'],
|
|
'userId' => $this->user_id,
|
|
'param1' => $this->user_id,
|
|
);
|
|
$sign = $this->getSign($params_sign);
|
|
$parts = http_build_query($params_sign);
|
|
$url = $this->url . "/v1/statistics/{$params['channelId']}/viewlog?{$parts}&sign={$sign}";
|
|
return $this->curlGet($url);
|
|
}
|
|
|
|
/**
|
|
* Notes:获取频道
|
|
* Created on: 2022/2/8 15:54
|
|
* Created by: dengbw
|
|
* @return bool|string //{"status":"success","result":[2797712]}
|
|
*/
|
|
function channels()
|
|
{
|
|
$sign = $this->getSign();
|
|
$url = $this->url . "/v1/users/" . $this->user_id . "/channels?appId=" . $this->app_id . "×tamp=" . $this->timestamp . "&sign=" . $sign;
|
|
return $this->curlGet($url);
|
|
}
|
|
|
|
/**
|
|
* Notes:获取签名
|
|
* Created on: 2022/2/8 15:50
|
|
* Created by: dengbw
|
|
* @param $params
|
|
* @return string
|
|
*/
|
|
function getSign($params = '')
|
|
{
|
|
if (!$params) {
|
|
$params = array(
|
|
'appId' => $this->app_id,
|
|
'timestamp' => $this->timestamp
|
|
);
|
|
}
|
|
// 1. 对加密数组进行字典排序
|
|
foreach ($params as $key => $value) {
|
|
$arr[$key] = $key;
|
|
}
|
|
sort($arr);
|
|
$str = $this->app_secret;
|
|
foreach ($arr as $k => $v) {
|
|
$str = $str . $arr[$k] . $params[$v];
|
|
}
|
|
$restr = $str . $this->app_secret;
|
|
$sign = strtoupper(md5($restr));
|
|
return $sign;
|
|
}
|
|
|
|
/**
|
|
* Notes:Get请求
|
|
* Created on: 2022/2/8 15:51
|
|
* Created by: dengbw
|
|
* @param string $url
|
|
* @return bool|string
|
|
*/
|
|
public function curlGet($url = '')
|
|
{
|
|
$ch = curl_init() or die (curl_error());
|
|
curl_setopt($ch, CURLOPT_URL, $url);
|
|
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
|
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 360);
|
|
$data = curl_exec($ch);
|
|
curl_close($ch);
|
|
return $data;
|
|
}
|
|
|
|
/**
|
|
* Notes:Post请求
|
|
* Created on: 2022/2/8 15:51
|
|
* Created by: dengbw
|
|
* @param string $url
|
|
* @param string $postData
|
|
* @param array $options
|
|
* @return bool|string
|
|
*/
|
|
public function curlPost($url = '', $postData = '', $options = array())
|
|
{
|
|
if (is_array($postData)) {
|
|
$postData = http_build_query($postData);
|
|
}
|
|
$ch = curl_init();
|
|
curl_setopt($ch, CURLOPT_URL, $url);
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
|
|
curl_setopt($ch, CURLOPT_POST, 1);
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
|
|
curl_setopt($ch, CURLOPT_TIMEOUT, 30); //设置cURL允许执行的最长秒数
|
|
if (!empty($options)) {
|
|
curl_setopt_array($ch, $options);
|
|
}
|
|
//https请求 不验证证书和host
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
|
|
$data = curl_exec($ch);
|
|
curl_close($ch);
|
|
return $data;
|
|
}
|
|
}
|
|
|