【PHP】心情愉悦(抖音小视频),可自行修改为接口,单文件运行!
本帖最后由 newClass 于 2023-5-15 10:56 编辑[*]新建Joyful.php,复制代码,直接访问即可
[*]自用共享,请勿倒卖
<?php
if ((isset($_SERVER["HTTP_X_REQUESTED_WITH"]) && strtolower($_SERVER["HTTP_X_REQUESTED_WITH"]) == "xmlhttprequest") || isset($_GET['jump'])) {
$starttime = microtime(1);
$result['run_time'] = number_format((microtime(1) - $starttime) * 1000, 1);
$ListApi = 'https://gitee.com/au_2023/codes/nxe5yoirc9l8s70dzvawh50/raw?blob_name=JoyfulList'; // 自己进行收集整理
$video_id = get_video_id($ListApi);
$result = array();
$result['play_addr'] = getRedirect('https://aweme.snssdk.com/aweme/v1/play/?video_id=' . $video_id);
$result['video_id'] = $video_id;
$result['run_time'] = number_format((microtime(1) - $starttime) * 1000, 1);
if (isset($_GET['jump'])) {
header('Location: ' . $result['play_addr']);
}
header("Content-Type: application/json");
echo json_encode($result);
exit('');
}
function get_video_id($Api)
{
$cache = __DIR__ . '/Joyful.txt';
if (is_file($cache)) {
$Data = unserialize(file_get_contents($cache));
if (count($Data) == 0) {
$Data = json_decode(file_get_contents($cache), true);
}
} else {
$Data = json_decode(file_get_contents($Api), true);
}
$key = rand(0, count($Data) - 1);
$video_id = $Data[$key];
unset($Data[$key]);
file_put_contents($cache, serialize(array_values($Data)));
return $video_id;
}
function getRedirect($Url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $Url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Accept: */*',
'Accept-Encoding: gzip',
'Connection: Keep-Alive',
'User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)'
));
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_NOBODY, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$ret = curl_exec($ch);
curl_close($ch);
preg_match("/Location: (.*?)\r\n/iU", $ret, $location);
if (isset($location)) {
return $location;
}
return false;
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>心情愉悦</title>
<meta name="renderer" content="webkit">
<meta name="referrer" content="never">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
<script src="https://cdn.bootcdn.net/ajax/libs/js-cookie/3.0.1/js.cookie.js"></script>
</head>
<body>
<div class="container" id="player" style="display: none;">
<video id="video" controls autoplay style="width: 100%;"></video>
</div>
<button id="start">开始运行</button>
</body>
<script>
load();
function start() {
var result = JSON.parse(Cookies.get('next'));
$('#player').show();
$('#start').hide();
$('#video').attr('src', result.play_addr);
load();
}
let video = document.getElementById('video');
video.onended = function() {
var result = JSON.parse(Cookies.get('next'));
$('#video').attr('src', result.new_play_addr);
load();
}
function load() {
$.ajax({
type: 'get',
url: 'Joyful.php',
async: false,
success: function(result) {
data = result;
downloadBlobFile('GET', result.play_addr).onreadystatechange = res => {
if (res.currentTarget.readyState == 4 && res.currentTarget.status == 200) {
result.new_play_addr = window.URL.createObjectURL(res.currentTarget.response);
result = JSON.stringify(result);
Cookies.set('next', result, 60);
}
}
},
error: function() {
//layer.msg('加载失败,等待下一次加载...');
}
});
return data;
}
function downloadBlobFile(_method, _url) {
const request = new XMLHttpRequest();
request.open(_method, _url);
request.send();
request.responseType = 'blob';
return request;
}
</script>
</html> # 来自@TabKey9,的第7个版
1. 楼主5月8日发帖挨删,我猜原因是`gitee`链接带Q号,提这件事只是想说,我从8号到12号,已经改了7个版了,~~后面应该不改了,能改的东西很少了,有个不算BUG的BUG:手速快的网友可以触发到控制台~~。吐槽一下,第一版复制下来根本用不了。~~开头没有`<?php`,变量声明用小写`$listApi`,使用的时候却用大写`$ListApi`。楼主第二次发帖时已经改过来了。~~
2. 可能编码习惯不同吧,如果`开封即用`不用改代码的情况下我是一点也不会动楼主代码的,但既然改了,那我不用单文件的形式,拆成`index.html`、`Joyful.php`,下面我分享对这两个文件的 **改动部分**:
### `Joyful.php`
> debug:调试代码的过程中抛出 一个报错`Uncaught TypeError: count(): Argument #1 ($value) must be of type Countable|array, null given in.....`、一个警告`Warning: Undefined array key 0 in......`
```php
function get_video_id($Api)
{
$cache = __DIR__ . '/Joyful.txt';
if (is_file($cache)) {
$Data = unserialize(file_get_contents($cache));
if (!is_array($Data)) {
$Data = json_decode(file_get_contents($cache), true);
if (!is_array($Data)) {
$Data = array();
}
}
} else {
$Data = json_decode(file_get_contents($Api), true);
}
if (empty($Data)) {
unlink($cache); // 删除缓存文件
$Data = json_decode(file_get_contents($Api), true);
}
$key = !empty($Data) ? rand(0, count($Data) - 1) : null;
$video_id = isset($Data[$key]) ? $Data[$key] : null;
if ($key !== null && $video_id !== null) {
unset($Data[$key]);
file_put_contents($cache, serialize(array_values($Data)));
}
return $video_id;
}
```
### `index.html`
改动太多,代码就不贴了,我留了很多很多的注释,偷个懒让我简单口述一下就好:
1. **特殊的需求:**嵌入第三方论坛,实现跨域名播放。
**难点:**跨域问题解决不了,因为跨域不能共享`cookie`。**改**!
**解决方案:**用浏览器的`本地存储`替代`cookie`,于是就有了下面的截图。
2. **最想要的需求:**上下滑动切换视频。
**难点:**首先我不会,但是我会曲线救国呀!我记得有个版本是在`<body>`加按钮的,丑到自己都嫌弃。
**解决方案:**还是按钮,只不过好看 **亿**点点了,首先引入一个国外的弹窗`UI`叫`sweetalert.min.js`,然后咱得有个`DOM事件监听器`,监听`H5 video`的播放\暂停,让这个`弹窗菜单`在视频被暂停的时候触发。然后就有了下面的效果,**完美**
@newClass 抱歉了,我把你的原创改的已经不一样了,不过我给你留了作者名哟!嘻嘻嘻
源码打包在此:
楼主发的代码有点问题啊;www,稍微修了下。
<?php
if ((isset($_SERVER["HTTP_X_REQUESTED_WITH"]) && strtolower($_SERVER["HTTP_X_REQUESTED_WITH"]) == "xmlhttprequest") || isset($_GET['jump'])) {
$starttime = microtime(1);
$result['run_time'] = number_format((microtime(1) - $starttime) * 1000, 1);
$listApi = 'https://gitee.com/au_2023/codes/nxe5yoirc9l8s70dzvawh50/raw?blob_name=JoyfulList'; // 自己进行收集整理
$video_id = get_video_id($listApi);
$result = array();
$result['play_addr'] = getRedirect('https://aweme.snssdk.com/aweme/v1/play/?video_id=' . $video_id);
$result['video_id'] = $video_id;
$result['run_time'] = number_format((microtime(1) - $starttime) * 1000, 1);
if (isset($_GET['jump'])) {
header('Location: ' . $result['play_addr']);
}
header("Content-Type: application/json");
echo json_encode($result);
exit('');
}
function get_video_id($Api)
{
$cache = __DIR__ . '/Joyful.txt';
if (is_file($cache)) {
$Data = unserialize(file_get_contents($cache));
if (count($Data) == 0) {
$Data = json_decode(file_get_contents($cache), true);
}
} else {
$Data = json_decode(file_get_contents($Api), true);
// var_dump($Data);exit();
}
$key = rand(0, count($Data) - 1);
$video_id = $Data[$key];
unset($Data[$key]);
file_put_contents($cache, serialize(array_values($Data)));
return $video_id;
}
function getRedirect($Url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $Url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Accept: */*',
'Accept-Encoding: gzip',
'Connection: Keep-Alive',
'User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)'
));
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_NOBODY, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$ret = curl_exec($ch);
curl_close($ch);
preg_match("/Location: (.*?)\r\n/iU", $ret, $location);
if (isset($location)) {
return $location;
}
return false;
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>心情愉悦</title>
<meta name="renderer" content="webkit">
<meta name="referrer" content="never">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<script src="https://cdn.staticfile.org/jquery/3.6.4/jquery.min.js"></script>
<script src="https://cdn.staticfile.org/js-cookie/3.0.5/js.cookie.min.js"></script>
</head>
<body>
<div class="container" id="player" style="display: none;">
<video id="video" controls autoplay style="width: 100%;"></video>
</div>
<button id="start">开始运行</button>
</body>
<script>
function start() {
var result = JSON.parse(Cookies.get('next'));
$('#player').show();
$('#start').hide();
$('#video').attr('src', result.play_addr);
load();
}
let video = document.getElementById('video');
video.onended = function() {
var result = JSON.parse(Cookies.get('next'));
$('#video').attr('src', result.new_play_addr);
load();
}
function load() {
$.ajax({
type: 'get',
url: 'Joyful.php',
async: false,
success: function(result) {
data = result;
downloadBlobFile('GET', result.play_addr).onreadystatechange = res => {
if (res.currentTarget.readyState == 4 && res.currentTarget.status == 200) {
result.new_play_addr = window.URL.createObjectURL(res.currentTarget.response);
result = JSON.stringify(result);
Cookies.set('next', result, 60);
}
}
},
error: function() {
//layer.msg('加载失败,等待下一次加载...');
}
});
return data;
}
function downloadBlobFile(_method, _url) {
const request = new XMLHttpRequest();
request.open(_method, _url);
request.send();
request.responseType = 'blob';
return request;
}
$(document).ready(function(){
load();
start();
});
</script>
</html> 不错不错,值得拥有{:1_899:} 没搞懂,新建了Joyful.php,然后打开网页只有一个开始运行按钮,点击没有任何反应,不知道是哪里出错了 <b>Warning</b>:array_values() expects parameter 1 to be array, boolean given in <b>/www/wwwroot/Joyful.php</b> on line <b>35</b><br />
{"play_addr":false,"video_id":null,"run_time":"746.0"}退出
提示这个错误 谢谢分享 htxz2022 发表于 2023-5-9 19:32
没搞懂,新建了Joyful.php,然后打开网页只有一个开始运行按钮,点击没有任何反应,不知道是哪里出错了
看看控制台有什么报错不 bdcpc 发表于 2023-5-9 22:13
Warning:array_values() expects parameter 1 to be array, boolean given in /www/wwwroot/Joyful.php o ...
我怀疑 你的服务器打不开https://gitee.com/au_2023/codes/nxe5yoirc9l8s70dzvawh50/raw?blob_name=JoyfulList这个链接 skykain 发表于 2023-5-9 23:04
楼主发的代码有点问题啊,稍微修了下。
{:1_893:}可以 但作用不大 谢谢分享,支持一下
页:
[1]
2