我的类直接从链接下载文件:
MyClass{
function download($link){
......
$ch = curl_init($link);
curl_setopt($ch, CURLOPT_FILE, $File->handle);
curl_setopt($ch,CURLOPT_WRITEFUNCTION , array($this,'__writeFunction'));
curl_exec($ch);
curl_close($ch);
$File->close();
......
}
function __writeFunction($curl, $data) {
return strlen($data);
}
}
我想知道如何在下载文件时使用CRULOPT_WRITEFUNCTION。 上面的代码,如果我删除行:
curl_setopt($ch,CURLOPT_WRITEFUNCTION , array($this,'__writeFunction'));
然后它会运行良好,我可以下载该文件。但如果我使用CURL_WRITEFUNCTION选项我无法下载文件。
答案 0 :(得分:4)
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_BUFFERSIZE, 8096);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, 'http://blog.ronnyristau.de/wp-content/uploads/2008/12/php.jpg');
$content = curl_exec($ch);
curl_close($ch);
$out = fopen('/tmp/out.png','w');
if($out){
fwrite($out, $content);
fclose($out);
}
答案 1 :(得分:4)
我知道这是一个老问题,但也许我的回答对你或其他人有所帮助。试试这个:
function get_write_function(){
return function($curl, $data){
return strlen($data);
}
}
我不知道你想要做什么,但是使用PHP 5.3,你可以用回调做很多事情。以这种方式生成函数的真正好处在于,通过'use'关键字传递的值后来保留在函数中,类似于常量。
function get_write_function($var){
$obj = $this;//access variables or functions within your class with the object variable
return function($curl, $data) use ($var, $obj) {
$len = strlen($data);
//just an example - you can come up with something better than this:
if ($len > $var){
return -1;//abort the download
} else {
$obj->do_something();//call a class function
return $len;
}
}
}
您可以将函数检索为变量,如下所示:
function download($link){
......
$var = 5000;
$write_function = $this->get_write_function($var);
$ch = curl_init($link);
curl_setopt($ch, CURLOPT_FILE, $File->handle);
curl_setopt($ch, CURLOPT_WRITEFUNCTION , $write_function);
curl_exec($ch);
curl_close($ch);
$File->close();
......
}
这只是一个例子。你可以在这里看到我如何使用它:Parallel cURL Request with WRITEFUNCTION Callback。我实际上没有测试所有这些代码,所以可能会有一些小错误。如果您遇到问题,请告诉我,我会解决它。
答案 2 :(得分:0)
为什么使用curl下载文件?有特殊原因吗?你可以简单地使用fopen和fread
我为它写了一个小班。
<?php
class Utils_FileDownload {
private $source;
private $dest;
private $buffer;
private $overwrite;
public function __construct($source,$dest,$buffer=4096,$overwrite=false){
$this->source = $source;
$this->dest = $dest;
$this->buffer = $buffer;
$this->overwrite = $overwrite;
}
public function download(){
if($this->overwrite||!file_exists($this->dest)){
if(!is_dir(dirname($this->dest))){mkdir(dirname($this->dest),0755,true);}
if($this->source==""){
$resource = false;
Utils_Logging_Logger::getLogger()->log("source must not be empty.",Utils_Logging_Logger::TYPE_ERROR);
}
else{ $resource = fopen($this->source,"rb"); }
if($this->source==""){
$dest = false;
Utils_Logging_Logger::getLogger()->log("destination must not be empty.",Utils_Logging_Logger::TYPE_ERROR);
}
else{ $dest = fopen($this->dest,"wb"); }
if($resource!==false&&$dest!==false){
while(!feof($resource)){
$read = fread($resource,$this->buffer);
fwrite($dest,$read,$this->buffer);
}
chmod($this->dest,0644);
fclose($dest); fclose($resource);
return true;
}else{
return false;
}
}else{
return false;
}
}
}
答案 3 :(得分:0)
一旦指定了CURLOPT_WRITEFUNCTION,似乎cURL使用您的函数而不是写入请求。
所以正确的解决方案是:
MyClass{
function download($link){
......
$ch = curl_init($link);
curl_setopt($ch, CURLOPT_FILE, $File->handle);
curl_setopt($ch,CURLOPT_WRITEFUNCTION , array($this,'__writeFunction'));
curl_exec($ch);
curl_close($ch);
$File->close();
......
}
function __writeFunction($curl, $data) {
echo $data;
return strlen($data);
}
}
这也可以处理二进制文件。