forked from xiongjian83/TvBox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
douyin.php
75 lines (69 loc) · 2.82 KB
/
douyin.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
69
70
71
72
73
74
75
<?php
$rid = empty($_GET['id']) ? "37917621268" : trim($_GET['id']);
$stream = empty($_GET['media']) ? "flv" : trim($_GET['media']);
function mk_dir($newdir)
{
$dir = $newdir;
if (is_dir('./' . $dir)) {
return $dir;
} else {
mkdir('./' . $dir, 0777, true);
return $dir;
}
}
function getDouYinUrl($rid, $stream,$cookietext)
{
$liveurl = "https://live.douyin.com/" . $rid;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $liveurl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookietext);
$response = curl_exec($ch);
preg_match('/__ac_nonce=(.*?);/', $response, $matches);
$ac_nonce = $matches[1];
curl_setopt($ch, CURLOPT_URL, $liveurl);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Cookie: __ac_nonce=" . $ac_nonce));
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookietext);
$response = curl_exec($ch);
preg_match('/ttwid=.*?;/', $response, $matches);
$ttwid = $matches[0];
$url = "https://live.douyin.com/webcast/room/web/enter/?aid=6383&app_name=douyin_web&live_id=1&device_platform=web&language=zh-CN&enter_from=web_live&cookie_enabled=true&screen_width=1728&screen_height=1117&browser_language=zh-CN&browser_platform=MacIntel&browser_name=Chrome&browser_version=116.0.0.0&web_rid=" . $rid;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36",
"Cookie: " . $ttwid,
"Accept: */*",
"Host: live.douyin.com",
"Connection: keep-alive"
));
$response = curl_exec($ch);
curl_close($ch);
$json = json_decode($response, true);
$status = $json['data']['data'][0]['status'];
if ($status != 2) {
return null;
}
$value = json_decode($json['data']['data'][0]['stream_url']['live_core_sdk_data']['pull_data']['stream_data'],true);
$realurl = "";
foreach ($value as $key => $val) {
if (array_key_exists('origin',$val)){
if ($stream == "flv") {
$realurl = $val['origin']['main']['flv'];
} elseif ($stream == "hls") {
$realurl = $val['data']['origin']['main']['hls'];
}
}
}
return $realurl;
}
$cookietext = './' . mk_dir('cookies/') . md5(microtime()) . '.' . 'txt';
$mediaurl = getDouYinUrl($rid, $stream,$cookietext);
unlink($cookietext);
if ($mediaurl!=null){
header('location:' . $mediaurl);
exit();
}