用php写一个功能访问这个httpcf228e76e6hk02hoomooncnapi接口把mp4视频文件下载到本地服务器存放文件名字用12位随机数字
以下为PHP代码实现:
<?php
$url = "http://cf228e76e6.hk02.hoomoon.cn/api/";
$filename = rand(100000000000, 999999999999) . ".mp4";
$file = fopen($filename, "w");
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FILE, $file);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($file);
echo "File downloaded successfully as $filename";
?>
解释:
- 首先定义了需要下载的接口 URL 和文件名字。
- 使用 rand() 函数生成一个 12 位随机数字作为文件名。
- 使用 fopen() 函数打开一个新文件。
- 初始化 cURL 并设置下载选项,包括 URL,文件句柄,以及禁用 HTTP header。
- 执行 cURL 请求并关闭连接。
- 关闭文件句柄。
- 输出成功消息,包括新文件名字。
原文地址: https://www.cveoy.top/t/topic/b4aE 著作权归作者所有。请勿转载和采集!