使用PHP和cURL提供下载服务
我正在编写一个脚本,使用我的Megaupload premium帐户为用户下载服务。这是我用来下载Megaupload文件的方法:使用PHP和cURL提供下载服务,php,curl,download,megaupload,Php,Curl,Download,Megaupload,我正在编写一个脚本,使用我的Megaupload premium帐户为用户下载服务。这是我用来下载Megaupload文件的方法: public function download_file($link) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $link); curl_setopt($ch, CURLOPT_COOKIEFILE, dirname(__FILE__) . "/cookies/megauploa
public function download_file($link)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $link);
curl_setopt($ch, CURLOPT_COOKIEFILE, dirname(__FILE__) . "/cookies/megaupload.txt");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
return curl_exec($ch);
curl_close($ch);
}
这就是在index.php中调用它的方式:
readfile($megaupload->download_file("http://www.megaupload.com/?d=PXNDZQHM"));
我基本上希望这样做的是将下载服务提供给用户,并通过我的服务器进行流式下载。我不希望文件保存到我的服务器,然后提供给用户
但显然,上述解决方案行不通。我得到这个错误:
Warning: readfile() [function.readfile]: Filename cannot be empty in C:\xampp\htdocs\index.php on line 8
我需要使用cURL,因为我需要使用cookie文件(/cookies/megaupload.txt)请求下载
有人能想出一个可行的方法来做到这一点吗?干杯。是的,正如它所说的,
readfile
需要一个文件名,而您要给它一个curl结果字符串。只需在以下情况下使用echo:
编辑:您还需要做一些基本检查,以确定您是否得到了一些东西:
public function download_file($link)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $link);
curl_setopt($ch, CURLOPT_COOKIEFILE, dirname(__FILE__) . "/cookies/megaupload.txt");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
// close before you return
curl_close($ch);
return $result;
}
然后:
使用你试图使用的方法不会起作用(或者不会像你想的那样起作用) Curl将一直阻止,直到文件完全下载 您必须创建自己的协议,并在类中处理写入事件 这里有一个链接到之前的讨论,其中有人做了很好的解释。基本上,您需要以可管理的块处理文件,并将其交给客户端浏览器: 您也可以尝试使用套接字,但您必须处理cookie代码,因为curl会在幕后为您处理很多这类代码。
- 如果用户是会员,则必须先登录
curl\u close($ch)代码>从不执行<代码>返回
从函数返回。
public function download_file($link)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $link);
curl_setopt($ch, CURLOPT_COOKIEFILE, dirname(__FILE__) . "/cookies/megaupload.txt");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
// close before you return
curl_close($ch);
return $result;
}
$filecontents = $megaupload->download_file("http://www.megaupload.com/?d=PXNDZQHM");
if($filecontents) {
echo $filecontents;
}
else {
die("Yikes, nothing here!");
}