CURL下载文件时如何使用CURLOPT_WRITEFUNCTION [英] How use CURLOPT_WRITEFUNCTION when download a file by CURL
本文介绍了CURL下载文件时如何使用CURLOPT_WRITEFUNCTION的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的类直接从链接下载文件:
My Class for download file direct from a link:
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。
上面的代码如果我删除行:
I want know how to use CRULOPT_WRITEFUNCTION when download file. Above code if i remove line:
curl_setopt($ ch,CURLOPT_WRITEFUNCTION,array($ this,'__ writeFunction'
curl_setopt($ch,CURLOPT_WRITEFUNCTION , array($this,'__writeFunction'));
然后它会运行良好,我可以下载该文件。但如果我使用CURL_WRITEFUNCTION选项,我不能下载文件。
Then it will run good, i can download that file.But if i use CURL_WRITEFUNCTION option i can't download file.
推荐答案
<?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);
}
这篇关于CURL下载文件时如何使用CURLOPT_WRITEFUNCTION的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文