哔哩哔哩视频网站是一个充满优质视频资源的平台。如果你想将这些精彩的视频分享到自己的网站上,我们提供了一个PHP代码片段,可以帮助你解析哔哩哔哩的视频并计算出最终输出的视频地址。此代理解析源码还带有防盗链功能,确保视频资源能够稳定地展示在您的网站上。如果您需要使用该代码,请按照以下步骤进行操作。
使用教程
注意:本规则,相对稳定,但是也不能保证百分之百一直有效,所以仅限会员用户使用!
解析源码
1.新建“bilibili.php”文件,复制下面代理规则代码
已获得查看权限
<?php
$domainList = "127.0.0.1,localhost,www.360mb.net,360mb.net";
$diymusic = '/daolian.mp3';
if (!in_array($_SERVER['HTTP_HOST'], explode(',', $domainList))) {
header('location: ' . $diymusic);
exit();
}
$id = isset($_GET['id']) ? addslashes($_GET['id']) : 'BV1264y1Y78s';
if (!preg_match('/^((BV|av)\w{10})$/', $id)) {
die('Invalid video id');
}
$data = curlRequest('https://m.bilibili.com/video/' . $id);
if (!$data) {
die('Failed to fetch video information');
}
preg_match('/"aid":(.*),"bvid":"(.*)","cid":(.*),"readyPoster":"(.*)","readyVideoUrl":"(.*)","readyDuration"/i', $data, $match);
$img = $match[4] ?? '';
$mp4 = $match[5] ?? '';
if (!$mp4) {
die('无法获得视频链接');
}
header('location: ' . $mp4);
exit();
function curlRequest($url) {
$curl = curl_init();
$headers = array(
"User-Agent: Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1"
);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HTTPHEADER,$headers);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_REFERER, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
$response = curl_exec($curl);
if (curl_errno($curl)) {
echo 'Curl error: ' . curl_error($curl);
curl_close($curl);
return false;
}
curl_close($curl);
return $response;
}
解析方式
1.访问“http(s)//你的域名/bilibili.php”
2.如无数据则调用默认的“BV11c41147zg”
3.调用播放:
http(s)//你的域名/bilibili.php?id=b站视频id
实例参考:
https://www.360mb.net/bilibili.php?id=BV11c41147zg
其他说明
如需使用JSON返回格式,则使用如下代码:
已获得查看权限
<?php
$domainList = "127.0.0.1,localhost,www.360mb.net,360mb.net";
$diymusic = '/daolian.mp3';
if (!in_array($_SERVER['HTTP_HOST'], explode(',', $domainList))) {
echo json_encode([
'code' => 200,
'message' => 'Success',
'data' => [
'url' => $diymusic
]
]);
exit();
}
$id = isset($_GET['id']) ? addslashes($_GET['id']) : 'BV1264y1Y78s';
if (!preg_match('/^((BV|av)\w{10})$/', $id)) {
echo json_encode([
'code' => 400,
'message' => 'Invalid video id',
'data' => [
'url' => ''
]
]);
exit();
}
$data = curlRequest('https://m.bilibili.com/video/' . $id);
if (!$data) {
echo json_encode([
'code' => 500,
'message' => 'Failed to fetch video information',
'data' => [
'url' => ''
]
]);
exit();
}
preg_match('/"aid":(.*),"bvid":"(.*)","cid":(.*),"readyPoster":"(.*)","readyVideoUrl":"(.*)","readyDuration"/i', $data, $match);
$img = $match[4] ?? '';
$mp4 = $match[5] ?? '';
if (!$mp4) {
echo json_encode([
'code' => 500,
'message' => 'Failed to get video link',
'data' => [
'url' => ''
]
]);
exit();
}
echo json_encode([
'code' => 200,
'message' => 'Success',
'data' => [
'url' => $mp4
]
]);
exit();
function curlRequest($url) {
$curl = curl_init();
$headers = array(
"User-Agent: Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1"
);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HTTPHEADER,$headers);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_REFERER, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
$response = curl_exec($curl);
if (curl_errno($curl)) {
echo 'Curl error: ' . curl_error($curl);
curl_close($curl);
return false;
}
curl_close($curl);
return $response;
}
返回实例如下:
{"code":200,"message":"Success","data":{"url":"https:\/\/cn-gddg-ct-01-10.bilivideo.com\/upgcxcode\/70\/20\/1406422070\/1406422070-1-16.mp4?e=ig8euxZM2rNcNbRVhwdVhwdlhWdVhwdVhoNvNC8BqJIzNbfq9rVEuxTEnE8L5F6VnEsSTx0vkX8fqJeYTj_lta53NCM=&uipk=5&nbs=1&deadline=1705654545&gen=playurlv2&os=bcache&oi=1730997412&trid=0000db16479eec1340dab143fa77736474d6h&mid=0&platform=html5&upsig=3015b077309bd49c49042286573626d6&uparams=e,uipk,nbs,deadline,gen,os,oi,trid,mid,platform&cdnid=61310&bvc=vod&nettype=0&f=h_0_0&bw=44056&logo=80000000"}}
本站声明: 1. 本站所有资源来源于网络,分享目的仅供大家学习和交流!
2. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
3. 本站提供的资源,都不包含技术服务请大家谅解!
4. 如有链接无法下载、失效或广告,请联系站长处理!
再次声明:如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。
佳益工作室 »
哔哩哔哩视频在线直链解析[含JSON解析方式]