Problem with my curl download script

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jeddiki
    Contributor
    • Jan 2009
    • 290

    #1

    Problem with my curl download script

    I am using curl to down load a zip file and I am
    then trying to unzip it, but I don't think that I am
    using the functions properly because I get warnings

    This is my code:

    Code:
    <?php 
    
    if (!extension_loaded('zip')) {
        dl('zip.so');
    }
    
    $func_path =  "/home/guru54gt5/public_html/im/my_functions.php";
    require_once("$func_path");
    
    $target_url = "http://www.support-focus.com/test_map.zip";
    $userAgent = 'Googlebot/2.1 (http://www.googlebot.com/bot.html)';
    
    echo "<br>Starting<br>Target_url: $target_url<br><br>";
    
    // make the cURL request to $target_url
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
    curl_setopt($ch, CURLOPT_URL,$target_url);
    curl_setopt($ch, CURLOPT_FAILONERROR, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_AUTOREFERER, true);
    curl_setopt($ch, CURLOPT_BINARYTRANSFER,true);
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    $page = curl_exec($ch);
    if (!$page) {
        echo "<br />cURL error number:" .curl_errno($ch);
        echo "<br />cURL error:" . curl_error($ch);
        exit;
    }
    curl_close($ch);
    
    // Un zip the file 
    
        $zip = new ZipArchive;
        if (! $zip) {
            echo "Could not make ZipArchive object.";
            exit;
        }
        $zip->open("$page");
        $zip->extractTo('./');
        $zip->close();
            echo "Ok!";
    ?>
    The errors I get are in fact warnings:

    Warning: ZipArchive::ext ractTo() [function.ZipArc hive-extractTo]: Invalid or unitialized Zip object in /home/guru54gt5/public_html/sys/convert_xml_no1 .php on line 40

    Warning: ZipArchive::clo se() [function.ZipArc hive-close]: Invalid or unitialized Zip object in /home/guru54gt5/public_html/sys/convert_xml_no1 .php on line 41
    Ok!

    Line 40 and 41 are:

    Code:
    $zip->extractTo('./');
    $zip->close();
    The script is here:

    test script

    Is the problem because my $page is not a file ?

    I am a bit confused about this so would appreciate some help.

    Thanks.
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    Hey.

    If you want the curl_exec function to actually return the results, you need to set the CURLOPT_RETURNT RANSFER option. If you do not, the results will be printed to the browser. (Which is why you get all those random characters on your page.)

    Two questions tho.
    1. Why do you use Curl for this? Couldn't you just as well use something like file_get_conten ts? (Assuming your configuration allows it to use URLs)
    2. Why do you have the request identify itself as Googlebot?

    Comment

    • jeddiki
      Contributor
      • Jan 2009
      • 290

      #3
      Hi,

      Thanks for your reply.

      I am using curl because I want to get used to using it and it is more powerful and faster than file_get_conten ts ( according to what I read)

      The Google_bot - well no reason really, it was in the code that
      I took as my example.

      I have moved forward with my script
      but I am still getting errors.

      This is what I have:


      Code:
      <?php 
      
      if (!extension_loaded('zip')) {
          dl('zip.so');
      }
      
      $func_path =  "/home/guru54gt5/public_html/im/my_functions.php";
      require_once("$func_path");
      
      $target_url = "http://www.support-focus.com/test_map.zip";
      $userAgent = 'Googlebot/2.1 (http://www.googlebot.com/bot.html)';
      $file_zip = "new.zip";
      $file_txt = "new.txt";
      
      echo "<br>Starting<br>Target_url: $target_url<br><br>";
      
      
      // make the cURL request to $target_url
      $ch = curl_init();
      $fp = fopen("$file_zip", "w"); 
      curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
      curl_setopt($ch, CURLOPT_URL,$target_url);
      curl_setopt($ch, CURLOPT_FAILONERROR, true);
      curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
      curl_setopt($ch, CURLOPT_AUTOREFERER, true);
      curl_setopt($ch, CURLOPT_BINARYTRANSFER,true);
      curl_setopt($ch, CURLOPT_TIMEOUT, 10);
      curl_setopt($ch, CURLOPT_FILE, $fp);
      
      $page = curl_exec($ch);
      
      
      if (!$page) {
      	echo "<br />cURL error number:" .curl_errno($ch);
      	echo "<br />cURL error:" . curl_error($ch);
      	exit;
      }
      curl_close($ch);
      
      echo "<br>Downloaded file: $target_url";
      echo "<br>Saved as file: $file_zip";
      echo "<br>About to unzip ...";
      
      // Un zip the file 
      
      $zip = new ZipArchive;
          if (! $zip) {
              echo "Could not make ZipArchive object.";
              exit;
          }
          $zip->open("$file_zip");
          $zip->extractTo("$file_txt");
          $zip->close();
      
      echo "<br>Unzipped file to: $file_txt<br><br>";		
      			
      ?>
      The result:

      Starting
      Target_url: http://www.support-focus.com/test_map.zip


      Downloaded file: http://www.support-focus.com/test_map.zip
      Saved as file: new.zip
      About to unzip ...
      Warning: ZipArchive::ext ractTo() [function.ZipArc hive-extractTo]: Invalid or unitialized Zip object in /home/guru54gt5/public_html/sys/convert_xml_no2 .php on line 52

      Warning: ZipArchive::clo se() [function.ZipArc hive-close]: Invalid or unitialized Zip object in /home/guru54gt5/public_html/sys/convert_xml_no2 .php on line 53

      Unzipped file to: new.txt
      Even though it says that it has unzipped the file,
      it doesn't. :confused:

      The new version is on: New Version

      Anyone know what I am doing wrong ?

      Comment

      • Atli
        Recognized Expert Expert
        • Nov 2006
        • 5062

        #4
        Ok, it seems the $zip->open() call is failing, but because you don't capture and test the return value of the method, the code just moves on and fails when you try to use the unopened zip object.

        You need to capture the error to see what is causing the problem.
        Try something like:
        [code=php]$zip = new ZipArchive();
        if($zip) {
        $result = $zip->open($file_zip );
        if($result === true) {
        $zip->extractTo($fil e_txt);
        $zip->close();
        echo "Success!";
        }
        else {
        echo "Failed to open file at '$file_zip'. Error code: " . $result;
        }
        }
        else {
        echo "Could not make ZipArchive object.";
        }
        $zip = null;[/code]

        P.S.
        You can see the error codes in the comments on the ZipArchive:open manual entry.

        Comment

        Working...