scottybwoy Posted March 30, 2009 Share Posted March 30, 2009 Hi, I have a script, which is meant to retrieve an image from an external server and create a local copy. However, at the moment all it is doing is creating a 1kb file of the filename, but it appears to be blank. Here's the script : function get_image_from_url($url, $filename) { if(function_exists('curl_init')) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $out = curl_exec($ch); curl_close($ch); if($out === false) { return false; } if($handle = fopen(DIR_FS_CATALOG_IMAGES . $filename, 'wb')) { fwrite($handle, $out); fclose($handle); } return true; } csv_import_message(CSV_CURL_MISSING_CANT_IMPORT_REMOTE_IMAGE, 'warning'); return false; } The url and filename are definitely passed to the function. And when putting the url directly into a browser, the image shows. Curl appears in info.php file like so : CURL support enabled CURL Information libcurl/7.10.6 OpenSSL/0.9.7a ipv6 zlib/1.2.3 And I'm using PHP Version 4.3.11 What could be causing this unexpected behaviour? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/151719-solved-curl-not-retrieving-image-properly/ Share on other sites More sharing options...
JonnoTheDev Posted March 30, 2009 Share Posted March 30, 2009 Why use CURL for this job. WGET far easier. exec("wget http://www.xyz.com/images/image.jpg"); Quote Link to comment https://forums.phpfreaks.com/topic/151719-solved-curl-not-retrieving-image-properly/#findComment-796768 Share on other sites More sharing options...
scottybwoy Posted March 30, 2009 Author Share Posted March 30, 2009 Sorry the reason it's using curl is because the remote images are stored like so : http://www.website.com/image.php?sku=AMDCPU392 Quote Link to comment https://forums.phpfreaks.com/topic/151719-solved-curl-not-retrieving-image-properly/#findComment-796781 Share on other sites More sharing options...
JonnoTheDev Posted March 30, 2009 Share Posted March 30, 2009 CURL will just browse to the page, not save the image. You should be using fopen() or file_get_contents() for this operation. Quote Link to comment https://forums.phpfreaks.com/topic/151719-solved-curl-not-retrieving-image-properly/#findComment-796813 Share on other sites More sharing options...
scottybwoy Posted March 30, 2009 Author Share Posted March 30, 2009 Sorry Neil, I did not write this script, but it WAS working before. Does fopen() and file_get_contents() work on remote sites? If you actually look at the code posted above, you will see that it also uses fopen/fwrite() to create the actual file based on the content returned by curl. The file is actually being written, but it just doesn't have any contents, so it seems my problem is with curl, even though it is installed and working properly. Also which method is faster, using curl or file_get_contents() and will file_get_contents() work when the webpage uses GET to retrieve the image? Can anyone see why the code above isn't working? Quote Link to comment https://forums.phpfreaks.com/topic/151719-solved-curl-not-retrieving-image-properly/#findComment-796858 Share on other sites More sharing options...
JonnoTheDev Posted March 30, 2009 Share Posted March 30, 2009 Just run a test with a url you have. Simple: $image = file_get_contents("http://www.google.co.uk/intl/en_uk/images/logo.gif"); print $image; // more code down here to save the image as a file on my server Quote Link to comment https://forums.phpfreaks.com/topic/151719-solved-curl-not-retrieving-image-properly/#findComment-796869 Share on other sites More sharing options...
JonnoTheDev Posted March 30, 2009 Share Posted March 30, 2009 The other thing that you code isn't doing is checking for curl errors $errors = curl_error($ch); You function should only try to save a local file if there are no errors. Your script just continues processing. For instance is your server able to resolve the url's that you are feeding in. You may have a dns issue on the server. The remote url may have blacklisted your server ip address. Could be anything. As I mentioned above I would not even use curl for this task. Quote Link to comment https://forums.phpfreaks.com/topic/151719-solved-curl-not-retrieving-image-properly/#findComment-796872 Share on other sites More sharing options...
scottybwoy Posted March 30, 2009 Author Share Posted March 30, 2009 Well I get a load of Un Readable junk if thats what you mean? Quote Link to comment https://forums.phpfreaks.com/topic/151719-solved-curl-not-retrieving-image-properly/#findComment-796873 Share on other sites More sharing options...
premiso Posted March 30, 2009 Share Posted March 30, 2009 That unreadable junk is the right stuff. It is the binary of the image. You write that to the file and it should show. Don't print it, as in order for the actual image to display after printing you would have to modify header to the image type. Try writing the $image from the file_get_contents to the file and see if that works. Quote Link to comment https://forums.phpfreaks.com/topic/151719-solved-curl-not-retrieving-image-properly/#findComment-796879 Share on other sites More sharing options...
scottybwoy Posted March 31, 2009 Author Share Posted March 31, 2009 Thanks, it is working now but does appear to be a lot slower. This script can occur thousands of times, so I did want it to be a quick as possible. Which method would be better? Quote Link to comment https://forums.phpfreaks.com/topic/151719-solved-curl-not-retrieving-image-properly/#findComment-797612 Share on other sites More sharing options...
JonnoTheDev Posted March 31, 2009 Share Posted March 31, 2009 Run as a background process, not through a web browser if slow. Should not really be slow, are you sure it isn't the remote site or your internet connection? Quote Link to comment https://forums.phpfreaks.com/topic/151719-solved-curl-not-retrieving-image-properly/#findComment-797613 Share on other sites More sharing options...
premiso Posted March 31, 2009 Share Posted March 31, 2009 file_get_contents is much slower than curl. I would figure out your curl issues if you want speed. Quote Link to comment https://forums.phpfreaks.com/topic/151719-solved-curl-not-retrieving-image-properly/#findComment-797710 Share on other sites More sharing options...
scottybwoy Posted April 1, 2009 Author Share Posted April 1, 2009 OK, Trying to solve this curl issue. Have added : $errors = curl_error($ch); echo "$errors<br />"; But it does not show any errors or get the image still. Any Ideas? Quote Link to comment https://forums.phpfreaks.com/topic/151719-solved-curl-not-retrieving-image-properly/#findComment-798626 Share on other sites More sharing options...
scottybwoy Posted April 3, 2009 Author Share Posted April 3, 2009 bump Quote Link to comment https://forums.phpfreaks.com/topic/151719-solved-curl-not-retrieving-image-properly/#findComment-800156 Share on other sites More sharing options...
premiso Posted April 3, 2009 Share Posted April 3, 2009 A simple test: <?php // create a new cURL resource $ch = curl_init(); // set URL and other appropriate options curl_setopt($ch, CURLOPT_URL, "http://www.google.com/intl/en_ALL/images/logo.gif"); curl_setopt($ch, CURLOPT_HEADER, 0); header("Content-Type: image/gif"); // grab URL and pass it to the browser curl_exec($ch); // close cURL resource, and free up system resources curl_close($ch); ?> If that does not display the google logo then there is something wrong with cURL on your webserver. I would look into re-installing or opening a ticket. Quote Link to comment https://forums.phpfreaks.com/topic/151719-solved-curl-not-retrieving-image-properly/#findComment-800390 Share on other sites More sharing options...
scottybwoy Posted April 3, 2009 Author Share Posted April 3, 2009 Thanks Premiso, But your above script worked fine, the Google image appears properly. Quote Link to comment https://forums.phpfreaks.com/topic/151719-solved-curl-not-retrieving-image-properly/#findComment-800419 Share on other sites More sharing options...
premiso Posted April 3, 2009 Share Posted April 3, 2009 Post your current code if you want further help. Quote Link to comment https://forums.phpfreaks.com/topic/151719-solved-curl-not-retrieving-image-properly/#findComment-800420 Share on other sites More sharing options...
scottybwoy Posted April 6, 2009 Author Share Posted April 6, 2009 Thanks Premiso, function get_image_from_url($url, $filename) { if(function_exists('curl_init')) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $out = curl_exec($ch); curl_close($ch); if($out === false) { return false; } if($handle = fopen(DIR_FS_CATALOG_IMAGES . $filename, 'wb')) { fwrite($handle, $out); fclose($handle); } return true; } csv_import_message(CSV_CURL_MISSING_CANT_IMPORT_REMOTE_IMAGE, 'warning'); return false; } I have checked the images exist by putting the passed url directly into the URI and also made sure that the filename and url are being passed to the function by echoing out each and they all appear. Thanks for your help. Quote Link to comment https://forums.phpfreaks.com/topic/151719-solved-curl-not-retrieving-image-properly/#findComment-802180 Share on other sites More sharing options...
premiso Posted April 6, 2009 Share Posted April 6, 2009 <?php if (get_image_from_url("http://www.google.com/intl/en_ALL/images/logo.gif", "logo.gif")) { echo "File retrieved succesfully. <img src=\"" . DIR_FS_CATALOG_IMAGES . "logo.gif\">"; }else { echo "File not retrieved."; } function get_image_from_url($url, $filename) { if(function_exists('curl_init')) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $out = curl_exec($ch); curl_close($ch); if($out === false) { echo "Unable to retrieve the image file." // remove this if it does not show up, should only be for debugging return false; } if($handle = fopen(DIR_FS_CATALOG_IMAGES . $filename, 'wb')) { fwrite($handle, $out); fclose($handle); }else { echo "Failed to write the image file"; // remove if this does not show up (here for debugging purposes only); return false; } return true; } csv_import_message(CSV_CURL_MISSING_CANT_IMPORT_REMOTE_IMAGE, 'warning'); return false; } ?> I do not know what your issue is. That worked out just fine for me. Maybe check out that the directory you are attempting to write the file to is valid/writeable. Quote Link to comment https://forums.phpfreaks.com/topic/151719-solved-curl-not-retrieving-image-properly/#findComment-802418 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.