云南那个因为你给的apiurl域名我这不可解析,所以就没弄,只给了杭州的php代码
<?php
function getHangzhouStream($item = []) {
$id = $item['id'] ?? 'hzzh';
$channelMap = [
'hzzh' => [16, 'hztv1'],
'hzmz' => [17, 'hztv2'],
'hzsh' => [18, 'hztv3'],
'hzys' => [21, 'hztv4'],
'hzqsty' => [20, 'hztv5'],
'hzds' => [22, 'hztv6'],
'fyxwzh' => [32, 'fyxwzh']
];
if (!isset($channelMap[$id])) {
return ['url' => 'ID不存在...'];
}
list($channelId, $channelCode) = $channelMap[$id];
$apiUrl = "https://mapi.hoolo.tv/api/v1/channel_detail.php?channel_id={$channelId}";
$apiRes = httpRequest($apiUrl, 'GET', ['Referer: https://tv.hoolo.tv/']);
if ($apiRes['code'] !== 200) {
return ['url' => '获取频道信息失败'];
}
$data = json_decode($apiRes['body'], true);
if (!isset($data[0]['m3u8'])) {
return ['url' => 'm3u8字段不存在'];
}
$m3u8Url = $data[0]['m3u8'];
$liveRes = httpRequest($m3u8Url, 'GET', ['Referer: https://tv.hoolo.tv/']);
if ($liveRes['code'] !== 200 || empty($liveRes['body'])) {
return ['url' => '无法获取播放列表'];
}
$liveContent = $liveRes['body'];
$streamPath = '';
if (in_array($id, ['hzzh', 'hzmz', 'hzsh'])) {
$hdPos = strpos($liveContent, 'hd');
if ($hdPos === false) {
return ['url' => '未找到HD流'];
}
$streamPath = trim(substr($liveContent, $hdPos));
} else {
$sdPos = strpos($liveContent, 'sd');
if ($sdPos === false) {
return ['url' => '未找到SD流'];
}
$sdContent = substr($liveContent, $sdPos);
$extPos = strpos($sdContent, '#EXT');
$streamPath = trim($extPos > 0 ? substr($sdContent, 0, $extPos) : $sdContent);
}
$uri = parse_url($m3u8Url);
$baseUrl = $uri['scheme'] . '://' . $uri['host'] . '/';
$channelPath = $baseUrl . $channelCode . '/';
$finalUrl = $channelPath . $streamPath;
$finalRes = httpRequest($finalUrl, 'GET', ['Referer: https://tv.hoolo.tv/']);
if ($finalRes['code'] !== 200 || empty($finalRes['body'])) {
return ['url' => '无法获取最终m3u8内容'];
}
$scriptUrl = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['SCRIPT_NAME'];
$proxyM3U8 = preg_replace_callback('/\/(.*?\.ts[^\s]*)/i', function($matches) use ($baseUrl, $scriptUrl) {
$tsUrl = $baseUrl . $matches[1];
return $scriptUrl . '?ts=' . urlencode($tsUrl);
}, $finalRes['body']);
return [
'm3u8' => $proxyM3U8,
'headers' => ['Referer' => 'https://tv.hoolo.tv/']
];
}
function httpRequest($url, $method = 'GET', $headers = []) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
if (!empty($headers)) {
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
}
$body = curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return ['code' => $code, 'body' => $body];
}
if (isset($_GET['ts'])) {
$tsUrl = $_GET['ts'];
$tsRes = httpRequest($tsUrl, 'GET', ['Referer: https://tv.hoolo.tv/']);
if ($tsRes['code'] === 200 && !empty($tsRes['body'])) {
header('Content-Type: video/MP2T');
header('Content-Length: ' . strlen($tsRes['body']));
echo $tsRes['body'];
} else {
header('HTTP/1.1 404 Not Found');
echo 'TS file not found';
}
exit;
}
$id = $_GET['id'] ?? 'hzzh';
$result = getHangzhouStream(['id' => $id]);
if (!empty($result['m3u8'])) {
header('Content-Type: application/vnd.apple.mpegurl; charset=utf-8');
echo $result['m3u8'];
} else {
header('Content-Type: text/plain; charset=utf-8');
echo $result['url'] ?? '未知错误';
}
?>
|