PHP Curl 下载 .tbz 和 .tbz.md5 扩展名的完整大文件

发布于 2024-12-10 16:24:39 字数 2850 浏览 0 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

东走西顾 2024-12-17 16:24:39

我认为问题很可能与回调写入函数有关。我不能完全弄清楚这应该达到什么目的,但它肯定不会给你带来任何好处。事实上,在我看来,那里有很多代码不需要您所描述的内容。

试试这个:

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

  }

?>

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:

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

  }

?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文