Jump to content

[SOLVED] Curl not retrieving image properly


scottybwoy

Recommended Posts

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

<?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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.