Php保利威直播示例 发表于 2021-12-30 | 更新于: 2022-05-13 | 分类于 学习 字数统计: 533 | 阅读时长 ≈ 2 前言: 参考地址 代码:123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120/** * 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'].'×tamp='.$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); }} Share