473,322 Members | 1,718 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Problem with my curl download script

290 100+
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.
Oct 30 '09 #1
3 3463
Atli
5,058 Expert 4TB
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?
Oct 30 '09 #2
jeddiki
290 100+
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:

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 ?
Oct 30 '09 #3
Atli
5,058 Expert 4TB
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.
Oct 31 '09 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Haluk Durmus | last post by:
Hello I checked out openssl,mm,apr,apr-util,apache 2,curl,libxml and php from cvs. php couse an ERROR I did the following steps:
0
by: Phil Powell | last post by:
What is the most standardized method of utilizing the CURL functions in PHP (version 4.3.2) to be able to retrieve the contents of a remote URL that happens to be dependent upon $_SESSION for its...
0
by: Chuck Anderson | last post by:
I am writing a Php script to run on my home PC (Windows) that downloads an Apache access log file and inserts new entries into a database.. The only way I can access these log files is through a...
6
by: Wescotte | last post by:
I'm writing a tiny php app that will log into our bank of america account and retrieve a file containing a list of checks that cleared the previous day. The problem I'm running into is when I...
0
by: nfhm2k | last post by:
I've been trying to find a solution to this for quite some time now... I even took a look at existing scripts... Including this one......
4
by: BinnyVA | last post by:
Hi, I am using PHP 5.1.2 with curl enabled. But whenever I try to use curl to fetch a url, it fails - 'curl_exec()' returns nothing. But if I try to execute the same file in CLI - like 'php...
4
by: Terry | last post by:
I'm using curl to invoke a php script on the same site/server. It works great, but if I call it again while it's still running, nothing happens. Why? Can that be fixed? Why use curl? To make...
0
by: xerc | last post by:
I am trying to create a generic function I can call to download all files from a single remote FTP directory -- using CURL. I want to multi-thread it, but need to get the single thread functionality...
11
by: Flexor | last post by:
I have a php script that runs from command line and makes an https request to paypal, using curl. It works fine if I run it from a web page. It fails if I run it from CLI. The error I get from...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.