捉取映客直播数据 20171121可用

<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/11/3
 * Time: 13:17
 * desc: 抓取映客热门直播主播,仅第一页
 * 原URL:http://baseapi.busi.inke.cn/live/LiveHotList
 */
namespace Library;

class AnchorYingKe
{
    public static function getYingKeAnchor($url)
    {
        $anchor_list = self::getAnchorList($url);
        $i = 0;
        foreach ($anchor_list as $v) {
            $ykuid = $v['third_party_uid'];
            $ykliveid = $v['third_party_room_id'];
            $live_stream_flv = self::getLiveStream($ykuid, $ykliveid);
            if (empty($live_stream_flv)) {
                continue;
            }else{
                $anchor_list[$i]['down_url'] = $live_stream_flv;
                $i++;
            }
        }

        return $anchor_list;
    }

    /**
     * POST 请求
     * @param string $url
     * @param array $param
     * @param boolean $post_file 是否文件上传
     * @return string content
     */
    public static function http_post($url, $param = [], $post_file = false, $isJson = false)
    {
        if (is_array($param) && count($param)) {
            $param = json_encode($param);
        }
        $oCurl = curl_init();
        if (stripos($url, "https://") !== FALSE) {
            curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, false);
            curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
        }
        if (is_string($param) || $post_file) {
            $strPOST = $param;
        } else {
            $aPOST = array();
            foreach ($param as $key => $val) {
                $aPOST[] = $key . "=" . urlencode($val);
            }
            $strPOST = join("&", $aPOST);
        }
        if (isset($_SESSION['CURLOPT_HTTPHEADER'])) {
            curl_setopt($oCurl, CURLOPT_HTTPHEADER, $_SESSION['CURLOPT_HTTPHEADER']);
        }
        if ($isJson) {
            curl_setopt($oCurl, CURLOPT_HTTPHEADER, array(
                    'Content-Type: application/json',
                    'Content-Length: ' . strlen($strPOST))
            );
        }
        curl_setopt($oCurl, CURLOPT_URL, $url);
        curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($oCurl, CURLOPT_POST, true);
        curl_setopt($oCurl, CURLOPT_POSTFIELDS, $strPOST);
        $sContent = curl_exec($oCurl);
        $aStatus = curl_getinfo($oCurl);
        curl_close($oCurl);

        if ($sContent) {
            return $sContent;
        } else {
            return false;
        }
    }

    // 映客的热门主播地址为:http://baseapi.busi.inke.cn/live/LiveHotList
    // 仅抓取第11-40个主播
    public static  function getAnchorList($url)
    {
        $html_info = self::http_post($url);
        $decode_html_info = json_decode($html_info,true);
        $anchor_list = [];
        if ($decode_html_info['message'] == 'success') {
            if($decode_html_info['data'][10]){
                $temp_anchor = [];
                for ($i = 11; $i <= 40; $i++) {
                    // 映客主播ID
                    $temp_anchor['third_party_uid'] = $decode_html_info['data'][$i]['uid'];
                    // 映客主播ID房间ID
                    $temp_anchor['third_party_room_id'] = $decode_html_info['data'][$i]['liveid'];
                    // 映客主播头像
                    $temp_anchor['avatar'] = $decode_html_info['data'][$i]['portrait'];
                    // 映客主播昵称
                    $temp_anchor['nick'] = $decode_html_info['data'][$i]['nick'];
                    // 映客直播标题
                    $temp_anchor['live_title'] = $decode_html_info['data'][$i]['name'];
                    // 映客主播性别
                    $temp_anchor['gender'] = $decode_html_info['data'][$i]['gender'];
                    // 映客直播地点
                    $temp_anchor['live_address'] = $decode_html_info['data'][$i]['city'];
                    array_push($anchor_list, $temp_anchor);
                }
            }else{
                $count = count($decode_html_info['data']);
                for ($i = 0; $i < $count; $i++) {
                    // 映客主播ID
                    $temp_anchor['third_party_uid'] = $decode_html_info['data'][$i]['uid'];
                    // 映客主播ID房间ID
                    $temp_anchor['third_party_room_id'] = $decode_html_info['data'][$i]['liveid'];
                    // 映客主播头像
                    $temp_anchor['avatar'] = $decode_html_info['data'][$i]['portrait'];
                    // 映客主播昵称
                    $temp_anchor['nick'] = $decode_html_info['data'][$i]['nick'];
                    // 映客直播标题
                    $temp_anchor['live_title'] = $decode_html_info['data'][$i]['name'];
                    // 映客主播性别
                    $temp_anchor['gender'] = $decode_html_info['data'][$i]['gender'];
                    // 映客直播地点
                    $temp_anchor['live_address'] = $decode_html_info['data'][$i]['city'];
                    array_push($anchor_list, $temp_anchor);
                }
            }

        }
        return $anchor_list;
    }

    // 映客获取单个主播地址为:http://baseapi.busi.inke.cn/live/LiveInfo?channel_id=&uid=71167152&liveid=&_t=
    public static  function getLiveStream($ykuid, $ykliveid)
    {
        $process = curl_init('http://baseapi.busi.inke.cn/live/LiveInfo?channel_id=&uid=' . $ykuid . '&liveid=' . $ykliveid . '&_t=');
        curl_setopt($process, CURLOPT_HEADER, 0);
        curl_setopt($process, CURLOPT_TIMEOUT, 30);
        curl_setopt($process, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($process, CURLOPT_FOLLOWLOCATION, 1);
        $return = curl_exec($process);
        curl_close($process);
        $decode_html_info = json_decode($return);
        $url = '';
        if ($decode_html_info->message = 'success') {
            if(isset($decode_html_info->data->live_addr[0])){
                if(isset($decode_html_info->data->live_addr[0]->stream_addr)){
                    $url = $decode_html_info->data->live_addr[0]->stream_addr;
                }
                /*if(isset($decode_html_info->data->live_addr[0]->rtmp_stream_addr)){
                    return $decode_html_info->data->live_addr[0]->rtmp_stream_addr;
                }*/
            }
        }

        //返回空即为已停止直播
        return $url;
    }
}

 

posted @ 2017-11-21 14:30  一个人的孤独自白  阅读(1421)  评论(0编辑  收藏  举报