我使用CURL从网址下载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);
?>
首先,我正在验证自己然后为所有四个循环我使用curl下载数据。我也从SO获得了这个卷曲代码。尝试了很多方法来做同样的事情。
任何帮助或想法都将受到高度赞赏。
更新
代码对我有用。我添加了curl_setopt($ ch,CURLOPT_RETURNTRANSFER,1);这两次首先它将进行身份验证并返回响应,然后从其他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);
答案 0 :(得分:0)
我认为问题很可能与回调写入功能有关。我可以完全解决这个应该实现的目标,但它肯定不会对你有所帮助。实际上,在我看来,那里有很多代码并不是你所描述的所必需的。
试试这个:
<?php
// Database connection - do you need this here? You never use it...
require ('dbconfig/dbconfig.php');
// While developing
ini_set('display_errors', 1);
error_reporting(E_ALL);
// The details of the destination
$username = "user";
$password = "password";
$urlbase = "http://domain-name/content/current/";
// Create the file list
$dateit = date("Ymd", strtotime('-1 day'));
$files = array("aaa$dateit.tbz", "aaa$dateit.tbz.md5", "bbb$dateit.tbz", "bbb$dateit.tbz.md5");
// Loop the file list
foreach ($files as $file) {
// Create pointers
$ch = curl_init($urlbase.$file);
$fp = fopen("adminArea/folder/$file", 'w+');
// Set cURL options
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);
curl_setopt($ch, CURLOPT_FILE, $fp);
// Do it
curl_exec ($ch);
// Close pointers
fclose($fp);
curl_close($ch);
}
?>