阿里云取出所有直播间的接口

上午一直在调试安卓同事的一个直播间接口

测试了好几种方法,最后快实现的时候,循环调用出现了问题

最后在 csdn上找到了解决方法

直接上代码吧,挺坑的,阿里的接口,也没注释,都不知道怎么做。


<?php
	header("Content-type: text/html; charset=utf-8"); 
	//phpinfo();
	function curl_http_request($url,$data = null)
	{
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
		//如果$data不为空,则为POST请求
		if (!empty($data)){
			curl_setopt($ch, CURLOPT_POST, 1);
			curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
		}
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		$output = curl_exec($ch);
		$error = curl_error($ch);
		curl_close($ch);
		if ($error){
			throw new Exception('请求发生错误:' . $error);
		}
		//$resultArr = json_decode($output, true);//将json转为数组格式数据
		return $output;

	}

	function percent_encode($res){
		$res=trim(utf8_encode(urlencode($res)));
		//$res=utf8_encode($res);
		$res=str_replace(array('+','*','%7E'), array('%20','%2A','~'), $res);
		return $res;
	}

	function uuid($prefix = '')
	{
		$chars = md5(uniqid(mt_rand(), true));
		$uuid  = substr($chars,0,8) . '-';
		$uuid .= substr($chars,8,4) . '-';
		$uuid .= substr($chars,12,4) . '-';
		$uuid .= substr($chars,16,4) . '-';
		$uuid .= substr($chars,20,12);
		return $prefix . $uuid;
	}

	function compute_signature($parameters, $access_key_secret){
		ksort($parameters);
		$canonicalizedQueryString = '';
		foreach ($parameters as $k => $v) {
			if(empty($canonicalizedQueryString))
				$canonicalizedQueryString .= percent_encode($k) . '=' . percent_encode($v);
			else
				$canonicalizedQueryString .= '&' . percent_encode($k) . '=' . percent_encode($v);
		}
		//var_dump($canonicalizedQueryString);
		$stringToSign = 'GET&%2F&' . percent_encode($canonicalizedQueryString);
		//var_dump($stringToSign);
		$signature=base64_encode(hash_hmac("sha1",$stringToSign,$access_key_secret."&",true));
		return $signature;
	}

	function compose_url($Action,$StreamName='',$AppName=''){
		//$timestamp=strftime("%Y-%m-%dT%H:%M:%SZ",time());
		$timestamp=gmdate("Y-m-d\TH:i:s\Z");
		$access_key_id='xxxxxxx';
		$access_key_secret='xxxxxxxxxxxxxxxxx';
		$parameters = array(
				'Format'        => 'JSON',
				'Version'       => '2014-11-11',
				'AccessKeyId'   => $access_key_id,
				'SignatureVersion'  => '1.0',
				'SignatureMethod'   => 'HMAC-SHA1',
				'SignatureNonce'    => uuid(),
				'TimeStamp'         => $timestamp,
				'Action'            =>$Action,//DescribeCdnService
				'DomainName'        =>'xxx.xxxxx.com',
				'AppName'        =>$AppName,
				'StreamName'        =>$StreamName,
				'StartTime'         => '',
				'EndTime'         =>'',

		);
		$signature = compute_signature($parameters, $access_key_secret);
		$canonicalizedQueryString='Signature='.urlencode($signature);
		foreach ($parameters as $k => $v) {
			$canonicalizedQueryString .= '&' . $k . '=' . percent_encode($v);
		}
		//var_dump("http://cdn.aliyuncs.com/?".$canonicalizedQueryString);


		$url = "http://cdn.aliyuncs.com/?".$canonicalizedQueryString;
		return $url;

		}




	//判断get参数是否存在
	if(is_array($_GET)&&count($_GET)>0)
	{
	
		$url_list=compose_url("DescribeLiveStreamsOnlineList");

		//第一个请求,获取参数
		//直播列表
		$result_lineList=array();
		//拼接url
		
		$array_string = curl_http_request($url_list);
		$result =json_decode($array_string);
		$object = json_decode(json_encode( $result),true);
		$res_array = $object["OnlineInfo"]["LiveStreamOnlineInfo"];
		if(empty($res_array)){
			$content = Array();
			$content["flag"]=0;
			$resturn_str = json_encode($content);
			echo $resturn_str;
		}else{
			$content = Array();
			$content["flag"]=1;
			$content["room"]=$res_array;
			//$resturn_str = json_encode($content);
			//echo $resturn_str;
			//循环遍历每个直播间

			for ($i= 0;$i< count($res_array); $i++){

				$SteamName = $res_array[$i]["StreamName"];
				//echo $SteamName;
				$url_num=compose_url('DescribeLiveStreamOnlineUserNum',$SteamName,'xxxx');

				$array_string_num = curl_http_request($url_num);
				$result_num = json_decode($array_string_num);
				$object_num = json_decode(json_encode( $result_num),true);

				//获取当前直播间的人数
				//$resturn_str = json_encode($object_num);
				//echo $resturn_str;
				//echo "<br/>";

				$UserNumber = $object_num["OnlineUserInfo"]["LiveStreamOnlineUserNumInfo"][0]["UserNumber"];
				$content["room"][$i]["UserNumber"]=$UserNumber;

				//$resturn_str = json_encode($content);
				//echo $resturn_str;

				//echo $content;

			}

			//得到最终数据
			$resturn_str = json_encode($content);
			echo $resturn_str;


		}

	}else{

		$url1=compose_url("DescribeLiveStreamsOnlineList");

		$url= compose_url('DescribeLiveStreamOnlineUserNum','StreamName1','xxxx');
		echo $url;

	}

	
?>




文章参考: http://blog.csdn.net/yfgcq/article/details/52169901
他把所有的功能都实现了, 直接使用就可以了!


原文链接: 阿里云取出所有直播间的接口 版权所有,转载时请注明出处,违者必究。
注明出处格式:流沙团 ( https://gyarmy.com/post-282.html )

发表评论

0则评论给“阿里云取出所有直播间的接口”