PHP Curl 下载 .tbz 和 .tbz.md5 扩展名的完整大文件
我正在使用 CURL 从 url 下载 4 个文件,我必须对其进行身份验证。我只使用 CURL 验证自己的身份。但问题是当我下载数据时,每个文件只下载 1KB。我尝试了SO上给出的各种方法。但他们都不为我工作。我的 CURL-PHP 代码是
<?php
require ('dbconfig/dbconfig.php'); //database connection.
ini_set('display_errors', 1);
error_reporting(E_ALL);
$username = "user";
$password = "password";
$url = "http://domain-name/contents/";
global $ch;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, Array("Content-Type: text/xml"));
curl_setopt($ch, CURLOPT_UNRESTRICTED_AUTH, 1);
$output = curl_exec($ch);
$r = time()-(24*60*60);
$dateit = date("Ymd", $r);
$file1 = "aaa".$dateit.".tbz";
$file2 = "aaa".$dateit.".tbz.md5";
$file3 = "bbb".$dateit.".tbz";
$file4 = "bbb".$dateit.".tbz.md5";
$arr = array($file1, $file2, $file3, $file4);
for($i = 0; $i <= 3; $i++) {
$url = "http://domain-name/content/current/".$arr[$i];
$writefn = function($ch, $chunk) {
static $data='';
static $limit = 500; // 500 bytes, it's only a test
$len = strlen($data) + strlen($chunk);
if ($len >= $limit ) {
$data .= substr($chunk, 0, $limit-strlen($data));
echo strlen($data) , ' ', $data;
return -1;
}
$data .= $chunk;
return strlen($chunk);
};
$ch = curl_init();
$fp = fopen("adminArea/folder/".$arr[$i], 'w+');
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RANGE, '0-500');
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
curl_setopt($ch, CURLOPT_WRITEFUNCTION, $writefn);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_exec ($ch);
}
fclose($fp);
curl_close($ch);
?>
首先我对自己进行身份验证,然后在 for 循环中,我使用curl 下载数据。我也从SO那里得到了这个curl代码。尝试了很多方法来做同样的事情。
任何帮助或想法将不胜感激。
更新
代码对我有用。我添加了curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);两次都会首先进行身份验证并返回响应,然后从其他网址为我下载文件。
// Set cURL options
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_UNRESTRICTED_AUTH, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
// Do it
curl_exec ($ch);
curl_setopt($ch, CURLOPT_URL, $urlbase.$file);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$fp = fopen("adminArea/folder/$file", 'w+');
curl_setopt($ch, CURLOPT_FILE, $fp);
// Do it
curl_exec ($ch);
// Close pointers
fclose($fp);
curl_close($ch);
I am using CURL to download 4 files from the url for which i have to authenticate myself. and i am authenticating myself with CURL only. But the problem is when i download data it only download 1KB of each file. I tried various methods given on SO. But none of them are working for me. My CURL-PHP code is
<?php
require ('dbconfig/dbconfig.php'); //database connection.
ini_set('display_errors', 1);
error_reporting(E_ALL);
$username = "user";
$password = "password";
$url = "http://domain-name/contents/";
global $ch;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, Array("Content-Type: text/xml"));
curl_setopt($ch, CURLOPT_UNRESTRICTED_AUTH, 1);
$output = curl_exec($ch);
$r = time()-(24*60*60);
$dateit = date("Ymd", $r);
$file1 = "aaa".$dateit.".tbz";
$file2 = "aaa".$dateit.".tbz.md5";
$file3 = "bbb".$dateit.".tbz";
$file4 = "bbb".$dateit.".tbz.md5";
$arr = array($file1, $file2, $file3, $file4);
for($i = 0; $i <= 3; $i++) {
$url = "http://domain-name/content/current/".$arr[$i];
$writefn = function($ch, $chunk) {
static $data='';
static $limit = 500; // 500 bytes, it's only a test
$len = strlen($data) + strlen($chunk);
if ($len >= $limit ) {
$data .= substr($chunk, 0, $limit-strlen($data));
echo strlen($data) , ' ', $data;
return -1;
}
$data .= $chunk;
return strlen($chunk);
};
$ch = curl_init();
$fp = fopen("adminArea/folder/".$arr[$i], 'w+');
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RANGE, '0-500');
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
curl_setopt($ch, CURLOPT_WRITEFUNCTION, $writefn);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_exec ($ch);
}
fclose($fp);
curl_close($ch);
?>
Firstly i am authenticating myself then in for loop for all four i am using curl to download data. I got this curl code too from SO. Tried a lot of ways to do the same.
Any help or idea will be highly appreciated.
UPDATE
code worked for me. I added curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); both times as firstly it will authenticate and return response and then download file for me from other url.
// Set cURL options
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_UNRESTRICTED_AUTH, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
// Do it
curl_exec ($ch);
curl_setopt($ch, CURLOPT_URL, $urlbase.$file);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$fp = fopen("adminArea/folder/$file", 'w+');
curl_setopt($ch, CURLOPT_FILE, $fp);
// Do it
curl_exec ($ch);
// Close pointers
fclose($fp);
curl_close($ch);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为问题很可能与回调写入函数有关。我不能完全弄清楚这应该达到什么目的,但它肯定不会给你带来任何好处。事实上,在我看来,那里有很多代码不需要您所描述的内容。
试试这个:
I think the problem is most likely to do with the callback write function. I can;t quite work out exactly what this was supposed to achieve as it is, but it certainly isn't doing you any favours. Indeed, it seems to me like there's a lot of code there that is not required for what you describe.
Try this: