簡體   English   中英

如何在CURL下載文件時使用CURLOPT_WRITEFUNCTION

[英]How use CURLOPT_WRITEFUNCTION when download a file by 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) {
                return strlen($data);            
          } 
}

我想知道如何在下載文件時使用CRULOPT_WRITEFUNCTION。 上面的代碼,如果我刪除行:

curl_setopt($ch,CURLOPT_WRITEFUNCTION , array($this,'__writeFunction'));

然后它會運行良好,我可以下載該文件。但如果我使用CURL_WRITEFUNCTION選項我無法下載文件。

我知道這是一個老問題,但也許我的回答對你或其他人有所幫助。 嘗試這個:

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();
    ......

}

那只是一個例子。 你可以在這里看到我如何使用它: 帶有WRITEFUNCTION回調的並行cURL請求 我實際上沒有測試所有這些代碼,所以可能會有一些小錯誤。 如果你有問題,請告訴我,我會解決它。

<?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);
}

一旦指定了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);            
      } 
}

這也可以處理二進制文件。

為什么使用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;
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM