豆因去水印api 根据楼下提供的代码改写
https://pic.callyulu.cn/i/2026/6e8462e460bec06367e47518ba6a2c97.png根据楼下提供的改写api
[*]<?php
// 定义一个关联数组来存储响应数据
$responseData = array(
'finalUrl' => '',
'message' => '请求成功',
'requestTime' => date("Y-m-d H:i:s"), // 添加请求时间
'requestStatus' => 'success', // 添加请求状态,默认为成功
);
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$input = $_POST['url'];
// 处理用户输入,尝试获取视/频ID或链接
$videoIdOrLink = processUserInput($input);
if (is_numeric($videoIdOrLink)) {
// 如果输入是纯数字,则认为它是videoId
$videoId = $videoIdOrLink;
} else if (preg_match('/v\.douyin\.com\/+/', $videoIdOrLink)) {
// 从链接中提取视/频ID
$videoId = extractVideoId($videoIdOrLink);
} else {
$responseData['message'] = "输入无法识别";
$responseData['requestStatus'] = 'error'; // 设置请求状态为错误
}
if ($videoId) {
$apiUrl = "https://www.iesdouyin.com/web/api/v2/aweme/iteminfo/?reflow_source=reflow_page&item_ids={$videoId}&a_bogus=64745b2b5bdc4e75b720a9a85b19867a";
$response = file_get_contents($apiUrl);
$data = json_decode($response, true);
if (!empty($data['item_list']['video']['play_addr']['uri'])) {
$uri = $data['item_list']['video']['play_addr']['uri'];
$desc = $data['item_list']['desc'];
$responseData['finalUrl'] = "www.iesdouyin.com/aweme/v1/play/?video_id={$uri}&ratio=1080p&line=0";
}
} else if (!$responseData['message']) {
$responseData['message'] = "无法获取视/频ID";
$responseData['requestStatus'] = 'error'; // 设置请求状态为错误
}
}
// 将响应数据以 JSON 格式返回
header('Content-Type: application/json');
echo json_encode($responseData);
function processUserInput($input) {
preg_match('/v\.douyin\.com\/+/', $input, $matches);
if (!empty($matches)) return $matches;
preg_match('/\d{19}/', $input, $matches);
if (!empty($matches)) return $matches;
return null;
}
function extractVideoId($link) {
$redirectLink = getRedirectUrl($link);
preg_match('/\/video\/(\d+)\//', $redirectLink, $idMatches);
return !empty($idMatches) ? $idMatches : null;
}
function getRedirectUrl($url) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
preg_match('/^Location: (.+)$/mi', $response, $matches);
return !empty($matches) ? trim($matches) : null;
}
?>
复制代码
https://pic.callyulu.cn/i/2026/6e8462e460bec06367e47518ba6a2c97.png
https://pic.callyulu.cn/i/2026/6e8462e460bec06367e47518ba6a2c97.png
纯数字也能当 videoId 处理,这容错做得还算用心。就是不知道真碰上带一堆参数的分享链接,还能不能认出来。
页:
[1]