Connecting Tech Pros Worldwide Help | Site Map

Problem with my curl download script

Familiar Sight
 
Join Date: Jan 2009
Posts: 165
#1: 3 Weeks Ago
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:

Expand|Select|Wrap|Line Numbers
  1. <?php 
  2.  
  3. if (!extension_loaded('zip')) {
  4.     dl('zip.so');
  5. }
  6.  
  7. $func_path =  "/home/guru54gt5/public_html/im/my_functions.php";
  8. require_once("$func_path");
  9.  
  10. $target_url = "http://www.support-focus.com/test_map.zip";
  11. $userAgent = 'Googlebot/2.1 (http://www.googlebot.com/bot.html)';
  12.  
  13. echo "<br>Starting<br>Target_url: $target_url<br><br>";
  14.  
  15. // make the cURL request to $target_url
  16. $ch = curl_init();
  17. curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
  18. curl_setopt($ch, CURLOPT_URL,$target_url);
  19. curl_setopt($ch, CURLOPT_FAILONERROR, true);
  20. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  21. curl_setopt($ch, CURLOPT_AUTOREFERER, true);
  22. curl_setopt($ch, CURLOPT_BINARYTRANSFER,true);
  23. curl_setopt($ch, CURLOPT_TIMEOUT, 10);
  24. $page = curl_exec($ch);
  25. if (!$page) {
  26.     echo "<br />cURL error number:" .curl_errno($ch);
  27.     echo "<br />cURL error:" . curl_error($ch);
  28.     exit;
  29. }
  30. curl_close($ch);
  31.  
  32. // Un zip the file 
  33.  
  34.     $zip = new ZipArchive;
  35.     if (! $zip) {
  36.         echo "Could not make ZipArchive object.";
  37.         exit;
  38.     }
  39.     $zip->open("$page");
  40.     $zip->extractTo('./');
  41.     $zip->close();
  42.         echo "Ok!";
  43. ?>
  44.  
  45.  
The errors I get are in fact warnings:

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

Warning: ZipArchive::close() [function.ZipArchive-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:

Expand|Select|Wrap|Line Numbers
  1. $zip->extractTo('./');
  2. $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's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,736
#2: 3 Weeks Ago

re: Problem with my curl download script


Hey.

If you want the curl_exec function to actually return the results, you need to set the CURLOPT_RETURNTRANSFER 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_contents? (Assuming your configuration allows it to use URLs)
  2. Why do you have the request identify itself as Googlebot?
Familiar Sight
 
Join Date: Jan 2009
Posts: 165
#3: 3 Weeks Ago

re: Problem with my curl download script


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_contents ( 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:


Expand|Select|Wrap|Line Numbers
  1. <?php 
  2.  
  3. if (!extension_loaded('zip')) {
  4.     dl('zip.so');
  5. }
  6.  
  7. $func_path =  "/home/guru54gt5/public_html/im/my_functions.php";
  8. require_once("$func_path");
  9.  
  10. $target_url = "http://www.support-focus.com/test_map.zip";
  11. $userAgent = 'Googlebot/2.1 (http://www.googlebot.com/bot.html)';
  12. $file_zip = "new.zip";
  13. $file_txt = "new.txt";
  14.  
  15. echo "<br>Starting<br>Target_url: $target_url<br><br>";
  16.  
  17.  
  18. // make the cURL request to $target_url
  19. $ch = curl_init();
  20. $fp = fopen("$file_zip", "w"); 
  21. curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
  22. curl_setopt($ch, CURLOPT_URL,$target_url);
  23. curl_setopt($ch, CURLOPT_FAILONERROR, true);
  24. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  25. curl_setopt($ch, CURLOPT_AUTOREFERER, true);
  26. curl_setopt($ch, CURLOPT_BINARYTRANSFER,true);
  27. curl_setopt($ch, CURLOPT_TIMEOUT, 10);
  28. curl_setopt($ch, CURLOPT_FILE, $fp);
  29.  
  30. $page = curl_exec($ch);
  31.  
  32.  
  33. if (!$page) {
  34.     echo "<br />cURL error number:" .curl_errno($ch);
  35.     echo "<br />cURL error:" . curl_error($ch);
  36.     exit;
  37. }
  38. curl_close($ch);
  39.  
  40. echo "<br>Downloaded file: $target_url";
  41. echo "<br>Saved as file: $file_zip";
  42. echo "<br>About to unzip ...";
  43.  
  44. // Un zip the file 
  45.  
  46. $zip = new ZipArchive;
  47.     if (! $zip) {
  48.         echo "Could not make ZipArchive object.";
  49.         exit;
  50.     }
  51.     $zip->open("$file_zip");
  52.     $zip->extractTo("$file_txt");
  53.     $zip->close();
  54.  
  55. echo "<br>Unzipped file to: $file_txt<br><br>";        
  56.  
  57. ?> 
  58.  
The result:

Quote:
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::extractTo() [function.ZipArchive-extractTo]: Invalid or unitialized Zip object in /home/guru54gt5/public_html/sys/convert_xml_no2.php on line 52

Warning: ZipArchive::close() [function.ZipArchive-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 ?
Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,736
#4: 3 Weeks Ago

re: Problem with my curl download script


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:
Expand|Select|Wrap|Line Numbers
  1. $zip = new ZipArchive();
  2. if($zip) {
  3.     $result = $zip->open($file_zip);
  4.     if($result === true) {
  5.         $zip->extractTo($file_txt);
  6.         $zip->close();
  7.         echo "Success!";
  8.     }
  9.     else {
  10.         echo "Failed to open file at '$file_zip'. Error code: " . $result;
  11.     }
  12. }
  13. else {
  14.     echo "Could not make ZipArchive object.";
  15. }
  16. $zip = null;
P.S.
You can see the error codes in the comments on the ZipArchive:open manual entry.
Reply