pili实时在线人数的统计

pili实时在线人数的统计

*这里我给了两个代码的使用案例

首先 我们来看看请求的api

 GET /v1/hubs/huxicongPili/stat/playcount HTTP/1.1
Host: pili.qiniuapi.com
Accept: */*
Content-Type: application/json
User-Agent: pili-sdk-php/1.5.4 curl/7.45.0 PHP/7.0.2
Authorization: Qiniu vI2xPIjOoh7udcRw4GdYNvf3o_gKsCx9wdZaC9u-:FIAbv5T1zlOLDaJDDo_L0tcaMj0=


123.059.063.003.00080-192.168.201.147.51277: HTTP/1.1 200 OK
Server: nginx/1.8.0
Date: Mon, 20 Feb 2017 02:51:35 GMT
Content-Type: application/json
Content-Length: 25
Connection: keep-alive
Access-Control-Allow-Origin: *
X-Log: PILI;PILI-LINA:2;PILI:192
X-Reqid: O0AAAJEuQIcd36QU
X-Reqid: O0AAAJEuQIcd36QU

{"total":1,"details":{"pili-live-rtmp.pilihu.echohu.top/huxicongPili/588773c868d03f791b030f4e":1}}

Authorization 的签名的方式

// 构造待签名的 Data
// 1. 添加 Path
data = " "
// 2. 添加 Query,前提: Query 存在且不为空
if "" != "" {
        data += "?"
}
// 3. 添加 Host
data += "\nHost: "
// 4. 添加 Content-Type,前提: Content-Type 存在且不为空
if "" != "" {
        data += "\nContent-Type: "
}
// 5. 添加回车
data += "\n\n"
// 6. 添加 Body,前提: Content-Length 存在且 Body 不为空,同时 Content-Type 存在且不为空或 "application/octet-stream"
bodyOK := "" != "" && "" != ""
contentTypeOK := "" != "" && "" != "application/octet-stream"
if bodyOK && contentTypeOK {
        data += ""
}
// 计算 HMAC-SHA1 签名,并对签名结果做 URL 安全的 Base64 编码
sign = hmac_sha1(data, "Your_Secret_Key")
encodedSign = urlsafe_base64_encode(sign)  
// 将 Qiniu 标识与 AccessKey、encodedSign 拼接得到管理凭证
<QiniuToken> = "Qiniu " + "Your_Access_Key" + ":" + encodedSign

我们来看返回的参数的含义

{"total":1,"details":{"pili-live-rtmp.pilihu.echohu.top/huxicongPili/588773c868d03f791b030f4e":1}}

tatal 是总的在线人数  
details  是每个流在直播的在线人数 例如  pili-live-rtmp.pilihu.echohu.top/huxicongPili/588773c868d03f791b030f4e  这个流有一个人 

下面我们来看看shell 的实现


#!/usr/bin/env bash
: '
create by sicong
'
if [ $# -lt 3 ]
then
    echo"参数错误 参数格式 AK SK HubName"
    exit 1
fi

reqpath="http://pili.qiniuapi.com"
#将要替换掉的转码的参数
#convertsP="flash_480p"
BASEPATHS="/v1/hubs/"
hub=$3
bashsc=${BASEPATHS}${hub}"/stat/playcount"
bks="GET "${bashsc}
PATHSS=${bks}"\nHost: pili.qiniuapi.com"
PATHSSS=${PATHSS}"\nContent-Type: application/json"
PATHSSSS=${PATHSSS}"\n\n"
PATHSSSSD=${PATHSSSS}
baseurl=${reqpath}${bashsc}
echo $baseurl
auth=$(echo -en $PATHSSSSD | openssl sha1 -hmac $2 -binary | base64 | tr "+/" "-_")
echo $auth
AUTHRON=$1":"${auth}
echo $AUTHRON
function HTTPPODST(){
    au=$1
    content="application/json"
    echo $content
    curl -i  $2  -H "Content-Type: "$content -H "Authorization: Qiniu "${au}  
}
HTTPPODST $AUTHRON $baseurl 

下面看看php 的代码


getplaycount();
function getplaycount()
{
    $HubName ="huxicongPili";//hub名字
    $url =getUrl($HubName);//获取请求的url
    $contentType ='application/json';
    $method =array("POST","GET");//设置请求的格式是post 还是get
    $ak = 'vI2xPIjOoh7udcRw4GdYNvf3o_gKsCx9wdZaC9u-'; //秘钥
    $sk = 'ja76Ne-wCvo-YSc88D3TwKM5O3JtBym5isn-YqjN'; //秘钥
    $da = MACToken($ak,$sk,$method[1],$url,$contentType);//获取Authorization的内容
    $va = "Authorization: ".$da;
    FunctionName($va,$url);//网络请求的打出
}

function getUrl($HubName){
//    GET /v1/hubs/huxicongPili/stat/playcount
    $baseURL = sprintf("%s//%s/%s/hubs/%s/stat/playcount", "http:", "pili.qiniuapi.com", "v1",$HubName);
    return $baseURL;
}
function FunctionName($va,$url,$xml_data =NULL)
{
// $url ='http://pili.qiniuapi.com/v2/hubs/1314xicong/streams/NTgyNDJlZTA1ZTc3YjAwODI5MDJiNzdh/converts';
    $header = "Content-Type: application/json";//定义content-type为xml
    $headerArr =array( "Content-Type: application/json","User-Agent: pili-sdk-php/1.5.4 curl/7.45.0 PHP/7.0.2",$va);
// $xml_data=array("converts"=>array("720p","480p"));
// $xml_data =json_encode($xml_data);
    $ch = curl_init(); //初始化curl

    curl_setopt($ch, CURLOPT_URL, $url);//设置链接
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//设置是否返回信息
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headerArr);//设置HTTP头

    if($xml_data){
        curl_setopt($ch, CURLOPT_POST, 1);//设置为POST方式
        curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_data);//POST数据
    }
    $response = curl_exec($ch);//接收返回信息
    if(curl_errno($ch)){//出错则显示错误信息
        print curl_error($ch);

    }
    curl_close($ch); //关闭curl链接
    echo $response;//显示返回信息
}

function base64UrlEncode($str)
{
    $find = array('+', '/');
    $replace = array('-', '_');
    return str_replace($find, $replace, base64_encode($str));
}

function base64UrlDecode($str)
{
    $find = array('-', '_');
    $replace = array('+', '/');
    return base64_decode(str_replace($find, $replace, $str));
}

function digest($secret, $data)
{
    return hash_hmac('sha1', $data, $secret, true);
}

function sign($secret, $data)
{
    return base64UrlEncode(digest($secret, $data));
}
function MACToken($ak,$sk,$method, $url, $contentType, $body)
{
    $url = parse_url($url);
    $data = '';
    if (!empty($url['path'])) {
        $data = $method . ' ' . $url['path'];
    }
    if (!empty($url['query'])) {
        $data .= '?' . $url['query'];
    }
    if (!empty($url['host'])) {
        $data .= "\nHost: " . $url['host'];
        if (isset($url['port'])) {
            $data .= ':' . $url['port'];
        }
    }
    if (!empty($contentType)) {
        $data .= "\nContent-Type: " . $contentType;
    }
    $data .= "\n\n";
    if (!empty($body)) {
        $data .= $body;
    }

    var_dump($data);
    return 'Qiniu ' . $ak . ':' . sign($sk, $data);
}

你可能感兴趣的:(七牛)