forked from pekkavaa/tuplain
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bilibili_api.php
68 lines (55 loc) · 1.74 KB
/
bilibili_api.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<?php
function requestBiliBiliAPI($bvid){
$ch = curl_init();
//get video information for AVID and CID
curl_setopt($ch, CURLOPT_URL, "https://api.bilibili.com/x/web-interface/view?bvid=" . $bvid);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
curl_close($ch);
$result = json_decode($result, true);
$aid = $result['data']['aid'];
$cid = $result['data']['cid'];
//get MP4 video address from AVID and CID
$playerUrl = "http://api.bilibili.com/x/player/playurl?platform=html5&qn=32&cid=" . $cid . "&avid=" . $aid;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $playerUrl);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
curl_close($ch);
$result = json_decode($result, true);
$video = $result['data']['durl']['0']['url'];
$videoArray = array(
"bvid" => $bvid,
"aid" => $aid,
"cid" => $cid,
"video" => $video,
"status" => "fresh"
);
return $videoArray;
}
header('Content-Type: application/json');
header("Access-Control-Allow-Origin: *");
if (!isset($_GET['bvid']) || empty($_GET['bvid']))
{
echo "{error:\"need bvid param\"}";
return;
}
$bvid = $_GET["bvid"];
$cacheKey = 'Bili_' . $bvid;
//Change this if you are using wincache
$use_wincache = false;
if ($use_wincache){
$videoArray = wincache_ucache_get($cacheKey, $success);
if ($success){
$videoArray["status"] = "cached";
}else{
$videoArray = requestBiliBiliAPI($bvid);
wincache_ucache_set($cacheKey, $videoArray, 600);
}
}else{
$videoArray = requestBiliBiliAPI($bvid);
}
echo json_encode($videoArray);
?>