PHP code example of wzj177 / short-video-parse
1. Go to this page and download the library: Download wzj177/short-video-parse library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
wzj177 / short-video-parse example snippets
$url = $_GET["url"] ?? "";
if (!filter_var($url, FILTER_VALIDATE_URL)) {
echo "url parameter needs to be set.";
exit();
}
// 动态设置 Referer
$refer = 'https://baidu.com';
if (false !== strpos($url, 'toutiao')) {
$refer = 'https://m.toutiao.com/';
} else if (false !== strpos($url, 'weibo')) {
$refer = 'https://weibo.com/';
} else if (false !== strpos($url, 'douyinvod.com')) {
$refer = 'https://www.douyin.com/';
}
// 1. 获取文件大小,改用 GET 请求获取响应头
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_NOBODY, false); // 不设置 HEAD 请求
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true); // 保留头信息
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36");
curl_setopt($ch, CURLOPT_REFERER, $refer);
curl_setopt($ch, CURLOPT_TIMEOUT, 10); // 避免长时间等待
$response = curl_exec($ch);
$info = curl_getinfo($ch);
$httpCode = $info['http_code'] ?? 0;
// 检查 HTTP 状态码
if ($httpCode !== 200) {
echo "Failed to fetch video: HTTP $httpCode.";
curl_close($ch);
exit();
}
// 提取文件大小
$filesize = $info['download_content_length'] ?? 0;
if ($filesize <= 0) {
echo "Failed to retrieve file size.";
curl_close($ch);
exit();
}
curl_close($ch);
// 2. 设置头信息
header('Content-Type: video/mp4');
$filename = basename(parse_url($url, PHP_URL_PATH)) ?: md5($url);
header('Content-Disposition: inline; filename="' . $filename . '"');
header('Content-Length: ' . $filesize);
header('Accept-Ranges: bytes');
// 3. 下载文件内容
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36");
curl_setopt($ch, CURLOPT_REFERER, $refer);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, false); // 直接输出给客户端
curl_exec($ch);
if (curl_errno($ch)) {
echo "CURL Error: " . curl_error($ch);
}
curl_close($ch);
`
例如抖音:$res = VideoManager::KuaiShou([
'proxy_whitelist' => ['kuaishou'],//白名单,需要提交类名,全部小写
'proxy' => '$ip:$port',
'url_validator' => [
这边参考config/url-validator.php
]
])->start($url);
可以参考config/url-validator.php的格式用参数传递,如果不指定则使用默认的
不会怎么编写全部使用默认也是可以的