<?php
$outerIP = '192.168.0.22';
$port = '8080';
$url = 'http://' . $outerIP . ':' . $port . '/stat';
$streamName = 'mystream';
echo $url . '</br>';
echo '查询的流:' . $streamName . '</br>';
var_dump(getStreamByIp($url, $streamName));//调用
//按照流的名称,获取该流的状态
function getStreamByIp($url, $streamName)
{
//查询录像模块的IP地址外网,根据这个可以查看到相应的流
// $url = $outerIP . "/rtmp/stat";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
$output = curl_exec($ch);
curl_close($ch);
//所有流的信息,解析Xml
$outputs = FromXml($output);
$streamInfo = $outputs['server']['application'][0]['live']['stream']; //主要看这里哦!!!!!!!!!!!!!!!!这里获取的时候是一个二维数组
if (!is_array($streamInfo)){
echo "没有正在运行的流";
exit;
}
if (array_key_exists("name", $streamInfo)) {
//判断该设备是否在这个数组中,真:获取这个设备的所有打流信息
if ($streamName == $streamInfo['name']) {
$totalInfo['status'] = 200;
$totalInfo['message'] = '服务正常,这个流正在运行';
$totalInfo['dataList']['name'] = $streamInfo['name'];
$totalInfo['dataList']['bw_in'] = $streamInfo['bw_in'];
$totalInfo['dataList']['bw_out'] = $streamInfo['bw_out'];
$totalInfo['dataList']['meta']['video']['width'] = $streamInfo['meta']['video']['width'];
$totalInfo['dataList']['meta']['video']['height'] = $streamInfo['meta']['video']['height'];
$totalInfo['dataList']['meta']['video']['frame_rate'] = $streamInfo['meta']['video']['frame_rate'];
$totalInfo['dataList']['meta']['video']['codec'] = $streamInfo['meta']['video']['codec'];
$totalInfo['dataList']['bw_video'] = $streamInfo['bw_video'];
} else {
$totalInfo['status'] = 500;
$totalInfo['message'] = '服务器不正常或者没有这个流';
$totalInfo['dataList']['name'] = $streamName;
$totalInfo['dataList']['bw_in'] = 0;
$totalInfo['dataList']['bw_out'] = 0;
}
} else {
//存放所有的设备号到一个数组中
foreach ($streamInfo as $key => $val) {
$deviceInfo[] = $val['name'];
}
//判断该设备是否在这个数组中,真:获取这个设备的所有打流信息
if (in_array($streamName, $deviceInfo)) {
$totalInfo['status'] = 200;
$totalInfo['message'] = 'The server is normal and is currently streaming';
foreach ($streamInfo as $val) {
if ($val['name'] == $streamName) {
$totalInfo['dataList']['name'] = $val['name'];
$totalInfo['dataList']['bw_in'] = $val['bw_in'];
$totalInfo['dataList']['bw_out'] = $val['bw_out'];
}
}
} else {
$totalInfo['status'] = 500;
$totalInfo['message'] = 'The server has a problem or is not currently streaming information 2';
$totalInfo['dataList']['name'] = $streamName;
$totalInfo['dataList']['bw_in'] = 0;
$totalInfo['dataList']['bw_out'] = 0;
}
}
return $totalInfo;
}
//解析xml
function FromXml($xml)
{
if (!$xml) {
$totalInfo['status'] = 500;
$totalInfo['message'] = '没有该设备的相应信息';
$totalInfo['dataList'] = null;
return $totalInfo;
}
//将XML转为array
$values = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
return $values;
}