MENU

php版 使用本地txt链接视频库 实现随机美女视频API

January 14, 2025 • Read: 16 • More

原版代码

<?php
// 请求参数说明:
// 参数名称    是否必填    备注内容
// json        是        json格式响应
// down        是        302重定向响应
// &id=        否        视频id编号

// 设置 CORS 头部,允许跨域访问(如果需要)
header("Access-Control-Allow-Origin: *");

// 设置响应内容类型为 JSON
header("Content-Type: application/json; charset=UTF-8");

// 视频库链接地址
$videoFilePath = "Beautyvideo/Beautyvideo.txt";

// 第三方API:用于获取视频链接
$thirdPartyApiUrl = "https://api.kxzjoker.cn/API/Beautyvideo/Beauty_video_url.php";

// 第三方解析API:用于解析视频链接
$thirdPartyParseApiUrl = "https://api.kxzjoker.cn/API/jiexi_video_2.php?url=";

// 获取请求参数
$type = isset($_GET['type']) ? trim($_GET['type']) : null;
$id = isset($_GET['id']) ? (int) $_GET['id'] : null;   

// 使用第三方API来获取视频链接
//Yes则使用第三方视频库,No则使用本地视频库
$useThirdPartyApi = 'Yes';  // 建议使用第三方视频库,第三方视频库每天都会更新几百条视频

// 校验 'type' 参数
if ($type === null) {
    echo json_encode(["msg" => "请提供 'type' 参数", "time" => date("Y-m-d H:i:s")], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
    exit;
}

// 处理第三方 API 请求
if ($useThirdPartyApi == 'Yes') {
    // 如果有 id 参数,附加到 API 请求的 URL 中
    $thirdPartyApiUrl = $id !== null ? $thirdPartyApiUrl . "?id=" . urlencode($id) : $thirdPartyApiUrl;

    // 获取第三方API的视频链接
    $thirdPartyResponse = @file_get_contents($thirdPartyApiUrl);

    if ($thirdPartyResponse === FALSE) {
        echo json_encode(["code" => 401, "msg" => "获取第三方视频链接失败,请联系管理员进行维护", "time" => date("Y-m-d H:i:s")], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
        exit;
    }

    $thirdPartyData = json_decode($thirdPartyResponse, true);

    if (isset($thirdPartyData['url'], $thirdPartyData['Video_platform'], $thirdPartyData['video_number'], $thirdPartyData['video_id'])) {
        $videoLink = $thirdPartyData['url'];
        $video_platform = $thirdPartyData['Video_platform'];
        $video_number = $thirdPartyData['video_number'];
        $video_id = $thirdPartyData['video_id'];

        // 解析视频链接
        $requestUrl = $thirdPartyParseApiUrl . urlencode($videoLink);
        $response = @file_get_contents($requestUrl);

        if ($response === FALSE) {
            echo json_encode(["code" => 401, "msg" => "解析请求失败,请联系管理员进行维护", "time" => date("Y-m-d H:i:s")], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
            exit;
        }

        $data = json_decode($response, true);

        if (isset($data['data']['download_url'], $data['data']['image_url'], $data['data']['video_title'])) {
            $download_url = $data['data']['download_url'];
            $image_url = $data['data']['image_url'];
            $video_title = $data['data']['video_title'];

            if ($type == 'json') {
                echo json_encode([
                    "code" => 200,
                    "video_title" => $video_title,
                    "download_url" => $download_url,
                    "image_url" => $image_url,
                    "Video_platform" => $video_platform,
                    "video_number" => $video_number,
                    "video_id" => $video_id,
                    "tips" => "接口由Wechat:Hello-byte提供",
                    "time" => date("Y-m-d H:i:s")
                ], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
            } elseif ($type == 'down') {
                header("Location: " . $download_url, true, 302);
                exit;
            } else {
                echo json_encode(["msg" => "无效的 'type' 参数", "time" => date("Y-m-d H:i:s")], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
            }
        } else {
            echo json_encode(["code" => 403, "msg" => "视频链接解析失败", "video_id" => $video_id, "time" => date("Y-m-d H:i:s")], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
        }
    } else {
        echo json_encode(["code" => 402, "msg" => "第三方API返回数据格式不正确", "time" => date("Y-m-d H:i:s")], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
    }

} else {
    // 使用本地文件 Beautyvideo.txt 作为视频库
    $videoLinks = @file($videoFilePath, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);

    if (empty($videoLinks)) {
        echo json_encode(["code" => 402, "msg" => "视频库有效链接失效,请联系网站管理员邮箱进行维护!", "tips" => "接口由Wechat:Hello-byte提供", "time" => date("Y-m-d H:i:s")], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
        exit;
    }

    // 选择视频链接
    if ($id !== null && $id >= 1 && $id <= count($videoLinks)) {
        $videoLink = $videoLinks[$id - 1];
        $video_id = $id;
    } else {
        $videoLink = $videoLinks[array_rand($videoLinks)];
        $video_id = array_search($videoLink, $videoLinks) + 1;
    }

    // 请求解析
    $requestUrl = $thirdPartyParseApiUrl . urlencode($videoLink);
    $response = @file_get_contents($requestUrl);

    if ($response === FALSE) {
        echo json_encode(["code" => 401, "msg" => "解析请求失败,请联系管理员进行维护!", "time" => date("Y-m-d H:i:s")], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
        exit;
    }

    $data = json_decode($response, true);

    if (isset($data['data']['download_url'], $data['data']['image_url'], $data['data']['video_title'])) {
        $download_url = $data['data']['download_url'];
        $image_url = $data['data']['image_url'];
        $video_title = $data['data']['video_title'];

        $video_platform = strpos($videoLink, 'kuaishou.com') !== false ? '快手' : (strpos($videoLink, 'douyin.com') !== false ? '抖音' : '未知平台');

        if ($type == 'json') {
            echo json_encode([
                "code" => 200,
                "video_title" => $video_title,
                "download_url" => $download_url,
                "image_url" => $image_url,
                "Video_platform" => $video_platform,
                "video_number" => count($videoLinks),
                "video_id" => $video_id,
                "tips" => "接口由Wechat:Hello-byte提供",
                "time" => date("Y-m-d H:i:s")
            ], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
        } elseif ($type == 'down') {
            header("Location: ". $download_url, true, 302);
            exit;
        } else {
            echo json_encode(["msg" => "无效的 'type' 参数", "time" => date("Y-m-d H:i:s")], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
        }
    } else {
        echo json_encode(["code" => 403, "msg" => "视频链接解析失败,请联系网站管理员邮箱进行维护!", "video_id" => $video_id, "time" => date("Y-m-d H:i:s")], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);

        // **仅在本地视频库中删除失效链接**
        // 删除失效链接的部分
        if ($useThirdPartyApi == 'No') {
            $indexToDelete = $video_id - 1;
            if ($indexToDelete >= 0 && $indexToDelete < count($videoLinks)) {
                array_splice($videoLinks, $indexToDelete, 1);
                file_put_contents($videoFilePath, implode("\n", $videoLinks));   
            }
        }
    }
}
?>

修改后的1

主要移除了使用第三方API,直接使用本地文件:/Beautyvideo/Beautyvideo.txt 作为视频库(要确保 /Beautyvideo/Beautyvideo.txt 文件存在且包含有效的视频链接,每行一个链接。)而且在原版基础上在 json_encode 函数中添加了 JSON_PRETTY_PRINT 标志来美化JSON输出。

<?php

// 设置 CORS 头部,允许跨域访问(如果需要)
header("Access-Control-Allow-Origin: *");

// 设置响应内容类型为 JSON
header("Content-Type: application/json; charset=UTF-8");

// 视频库链接地址
$videoFilePath = "Beautyvideo/Beautyvideo.txt";

// 第三方API:用于获取视频链接
$thirdPartyApiUrl = "https://api.kxzjoker.cn/API/Beautyvideo/Beauty_video_url.php";

// 获取请求参数
$type = isset($_GET['type']) ? trim($_GET['type']) : null;
$id = isset($_GET['id']) ? (int) $_GET['id'] : null;   

// 校验 'type' 参数
if ($type === null) {
    echo json_encode(["msg" => "请提供 'type' 参数", "time" => date("Y-m-d H:i:s")], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT);
    exit;
}

// 使用本地文件 Beautyvideo.txt 作为视频库
$videoLinks = @file($videoFilePath, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);

if (empty($videoLinks)) {
    echo json_encode(["code" => 402, "msg" => "视频库有效链接失效,请联系网站管理员邮箱进行维护!", "time" => date("Y-m-d H:i:s")], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT);
    exit;
}

// 选择视频链接
if ($id !== null && $id >= 1 && $id <= count($videoLinks)) {
    $videoLink = $videoLinks[$id - 1];
    $video_id = $id;
} else {
    $videoLink = $videoLinks[array_rand($videoLinks)];
    $video_id = array_search($videoLink, $videoLinks) + 1;
}

// 判断视频平台
$video_platform = strpos($videoLink, 'kuaishou.com') !== false ? '快手' : (strpos($videoLink, 'douyin.com') !== false ? '抖音' : '未知平台');

// 返回JSON格式响应
if ($type == 'json') {
    echo json_encode([
        "code" => 200,
        "url" => $videoLink,
        "Video_platform" => $video_platform,
        "video_number" => count($videoLinks),
        "video_id" => $video_id,
        "tips" => "接口由Wechat:Hello-byte提供",
        "time" => date("Y-m-d H:i:s")
    ], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT);
} elseif ($type == 'down') {
    header("Location: " . $videoLink, true, 302);
    exit;
} else {
    echo json_encode(["msg" => "无效的 'type' 参数", "time" => date("Y-m-d H:i:s")], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT);
}
?>

修改后的2

主要设置了默认是:json方式访问,实现默认访问http://v.syylw.com/api/Beauty_video.php 时即以json类型 访问,在PHP脚本 中设置一个默认值,使得当请求中没有提供 type参数 时,自动使用 json 作为默认值。

<?php

// 设置 CORS 头部,允许跨域访问(如果需要)
header("Access-Control-Allow-Origin: *");

// 设置响应内容类型为 JSON
header("Content-Type: application/json; charset=UTF-8");

// 视频库链接地址
$videoFilePath = "Beautyvideo/Beautyvideo.txt";

// 获取请求参数
$type = isset($_GET['type']) ? trim($_GET['type']) : 'json'; // 设置默认值为 'json'
$id = isset($_GET['id']) ? (int) $_GET['id'] : null;   

// 使用本地文件 Beautyvideo.txt 作为视频库
$videoLinks = @file($videoFilePath, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);

if (empty($videoLinks)) {
    echo json_encode(["code" => 402, "msg" => "视频库有效链接失效,请联系网站管理员邮箱进行维护!", "time" => date("Y-m-d H:i:s")], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT);
    exit;
}

// 选择视频链接
if ($id !== null && $id >= 1 && $id <= count($videoLinks)) {
    $videoLink = $videoLinks[$id - 1];
    $video_id = $id;
} else {
    $videoLink = $videoLinks[array_rand($videoLinks)];
    $video_id = array_search($videoLink, $videoLinks) + 1;
}

// 判断视频平台
$video_platform = strpos($videoLink, 'kuaishou.com') !== false ? '快手' : (strpos($videoLink, 'douyin.com') !== false ? '抖音' : '未知平台');

// 返回JSON格式响应
if ($type == 'json') {
    echo json_encode([
        "code" => 200,
        "url" => $videoLink,
        "Video_platform" => $video_platform,
        "video_number" => count($videoLinks),
        "video_id" => $video_id,
        "tips" => "接口由Wechat:Hello-byte提供",
        "time" => date("Y-m-d H:i:s")
    ], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT);
} elseif ($type == 'down') {
    header("Location: " . $videoLink, true, 302);
    exit;
} else {
    echo json_encode(["msg" => "无效的 'type' 参数", "time" => date("Y-m-d H:i:s")], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT);
}
?>

在这个修改后的代码中,我将获取 type参数 的逻辑修改为:

$type = isset($_GET['type']) ? trim($_GET['type']) : 'json'; // 设置默认值为 'json'

这意味着如果请求中没有提供 type参数$type 将自动被设置为'json'。这样,当用户访问 http://v.syylw.com/api/Beauty_video.php 时,如果没有指定 type参数,系统将默认以 json类型 进行响应。

原版API【随机美女视频视频库】

请求说明:

请求地址:https://api.kxzjoker.cn/API/Beautyvideo/Beauty_video_url.php
返回格式:JSON
请求方式:GET/POST
请求示例:https://api.kxzjoker.cn/API/Beautyvideo/Beauty_video_url.php

返回示例:

{
    "code": 200,
    "url": "https://v.kuaishou.com/dmOrEd",
    "Video_platform": "快手",
    "video_number": 1933,
    "video_id": 59,
    "tips": "接口由看戏仔-API(http://api.kxzjoker.cn/)免费提供",
    "time": "2024-12-25 21:55:54"
}

请求参数:

参数名称是否必填备注内容
id视频id编号

返回参数:

参数名称参数类型参数说明
urlstring视频地址
Video_platformstring视频平台
video_numberstring当前接口视频数量
video_idstring视频id编号

访问返回:

{
    "code": 200,
    "url": "https://v.kuaishou.com/imxxeZ",
    "Video_platform": "快手",
    "video_number": 12208,
    "video_id": 12033,
    "tips": "接口由看戏仔-API(http://api.kxzjoker.cn/)免费提供",
    "time": "2025-01-14 15:22:09"
}

原版API【聚合短视频解析(线路2)】

请求说明:

请求地址:https://api.kxzjoker.cn/API/jiexi_video_2.php
返回格式:JSON/MP4
请求方式:GET/POST
请求示例:https://api.kxzjoker.cn/API/jiexi_video_2.php?url=https://v.douyin.com/i2q93e3N/

返回示例:

{
    "success": true,
    "data": {
        "video_title": "定格美好瞬间~#旅行#转场#甜妹",
        "video_url": "https://v9-default.365yg.com/ba4e13cee3720eaeaefac247d54e34c6/676bfea3/video/tos/cn/tos-cn-ve-15/ogsAHdnXeCAEOB7bp9GKABzApcOIEDQbgmweXD/?a=2011&br=1675&bt=1675&btag=80000e00008000&cd=0%7C0%7C0%7C0&ch=0&cr=0&cs=0&cv=1&dr=0&ds=4&dy_q=1735127175&dy_va_biz_cert=&feature_id=46a7bb47b4fd1280f3d3825bf2b29388&ft=k7Fz7VVywhiRF_80mo~pK7pswApoiT6ZvrK40zTmto0g3cI&l=202412251946156AD44F7BFBC7EE19A41E&mime_type=video_mp4&net=5&qs=0&rc=NDZpN2Q2ZjZkZDczZDc2PEBpMzN1aWg6ZmtqcjMzNGkzM0AwMC8yYDAuNS8xNmMyNTQuYSMyM2VecjQwMHFgLS1kLTBzcw%3D%3D",
        "download_url": "https://v9-default.365yg.com/ba4e13cee3720eaeaefac247d54e34c6/676bfea3/video/tos/cn/tos-cn-ve-15/ogsAHdnXeCAEOB7bp9GKABzApcOIEDQbgmweXD/?a=2011&br=1675&bt=1675&btag=80000e00008000&cd=0%7C0%7C0%7C0&ch=0&cr=0&cs=0&cv=1&dr=0&ds=4&dy_q=1735127175&dy_va_biz_cert=&feature_id=46a7bb47b4fd1280f3d3825bf2b29388&ft=k7Fz7VVywhiRF_80mo~pK7pswApoiT6ZvrK40zTmto0g3cI&l=202412251946156AD44F7BFBC7EE19A41E&mime_type=video_mp4&net=5&qs=0&rc=NDZpN2Q2ZjZkZDczZDc2PEBpMzN1aWg6ZmtqcjMzNGkzM0AwMC8yYDAuNS8xNmMyNTQuYSMyM2VecjQwMHFgLS1kLTBzcw%3D%3D",
        "image_url": "https://p11-sign.douyinpic.com/tos-cn-i-0813/ocDHztBsAhnDIdA6rAzgfC47E7eYAAduAAkA58~c5_300x400.jpeg?lk3s=138a59ce&x-expires=1736334000&x-signature=elDP1ycPh0oTuSthnNEAcxxG4pE%3D&from=327834062_large&s=PackSourceEnum_AWEME_DETAIL&se=false&sc=cover&biz_tag=aweme_video&l=2024122519461599C49874E8266D13A8F8"
    },
    "tips": "接口由看戏仔-API(http://api.kxzjoker.cn/)免费提供",
    "time": "2024-12-25 19:46:16"
}

请求参数:

参数名称是否必填备注内容
url短视频作品链接
type=down直接播放无水印视频

返回参数:

参数名称参数类型参数说明
video_titlestring返回作品描述或标题
video_urlstring视频链接
download_urlstring视频下载链接
image_urlstring返回封面图片链接

访问返回:

{
    "success": 200,
    "data": {
        "video_title": "定格美好瞬间~#旅行#转场#甜妹",
        "video_url": "https://v9-default.365yg.com/1679d5cd6aecad83c0b731d1c0fda888/67862095/video/tos/cn/tos-cn-ve-15/ogsAHdnXeCAEOB7bp9GKABzApcOIEDQbgmweXD/?a=0&ch=0&cr=0&dr=0&net=5&cd=0%7C0%7C0%7C0&cv=1&br=1675&bt=1675&cs=0&ds=4&ft=k7Fz7VVywhiRF_80mo~pK7pswApt-o0ZvrK40zTmto0g3cI&mime_type=video_mp4&qs=0&rc=NDZpN2Q2ZjZkZDczZDc2PEBpMzN1aWg6ZmtqcjMzNGkzM0AwMC8yYDAuNS8xNmMyNTQuYSMyM2VecjQwMHFgLS1kLTBzcw%3D%3D&btag=80000e00008000&dy_q=1736839801&feature_id=46a7bb47b4fd1280f3d3825bf2b29388&l=2025011415300171284B75F58D3E465D5C",
        "download_url": "https://v9-default.365yg.com/1679d5cd6aecad83c0b731d1c0fda888/67862095/video/tos/cn/tos-cn-ve-15/ogsAHdnXeCAEOB7bp9GKABzApcOIEDQbgmweXD/?a=0&ch=0&cr=0&dr=0&net=5&cd=0%7C0%7C0%7C0&cv=1&br=1675&bt=1675&cs=0&ds=4&ft=k7Fz7VVywhiRF_80mo~pK7pswApt-o0ZvrK40zTmto0g3cI&mime_type=video_mp4&qs=0&rc=NDZpN2Q2ZjZkZDczZDc2PEBpMzN1aWg6ZmtqcjMzNGkzM0AwMC8yYDAuNS8xNmMyNTQuYSMyM2VecjQwMHFgLS1kLTBzcw%3D%3D&btag=80000e00008000&dy_q=1736839801&feature_id=46a7bb47b4fd1280f3d3825bf2b29388&l=2025011415300171284B75F58D3E465D5C",
        "image_url": "https://p11-sign.douyinpic.com/tos-cn-i-0813/ocDHztBsAhnDIdA6rAzgfC47E7eYAAduAAkA58~c5_300x400.jpeg?lk3s=138a59ce&x-expires=1738047600&x-signature=e%2BYpC4c5XvU5IRTczMpByWUPiXI%3D&from=327834062_large&s=PackSourceEnum_AWEME_DETAIL&se=false&sc=cover&biz_tag=aweme_video&l=202501141530016881D2CACDB1180188E1"
    },
    "tips": "接口由看戏仔-API(http://api.kxzjoker.cn/)免费提供",
    "time": "2025-01-14 15:30:00"
}


Archives QR Code Tip
QR Code for this page
Tipping QR Code