Forum Moderators: coopster

Message Too Old, No Replies

save image from other site on my server using curl.

         

masterpp

10:13 pm on Jun 23, 2003 (gmt 0)

10+ Year Member



I need a sample code how to grab the image from other site and put the image into my server and rename it.

$ch = curl_init("http://www.othersite.com/image.jpg");
...
...

jatar_k

3:42 am on Jun 24, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



you dont necessarily need to use curl, you could use fopen and a couple of PHP Image Functions [ca.php.net]

daisho

1:37 pm on Jun 24, 2003 (gmt 0)

10+ Year Member



Here is a snipet of code that I use. This doesn't write out to disk (haven't added caching yet) but as you can see the variable $rawdata contains the image. You can simply use fopen fwrite fclose to write it out to a file.

The $url parameter is the full url of the image you want to pull.

daisho.

function ShowImage ($url) {
$ch = curl_init ($url);

$img=NULL;

curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);

$rawdata=curl_exec ($ch);
curl_close ($ch);

if(!$rawdata) {
SendDefaultImage();
} else {

$ct=content_type($rawdata);
if( "Content-Type: unknown"==$ct ) {
SendDefaultImage();
} else {
Header( $ct );
print $rawdata;
}
}
}

masterpp

4:08 pm on Jun 24, 2003 (gmt 0)

10+ Year Member



i received this error using your code.

Fatal error: Call to undefined function: content_type().

i have php 4.2.3.

It doesn't have to be curl. it can be anything.

I tried to use this
$url = file_get_contents("http://www.php.net/");
$fp = fopen("php_homepage.txt", 'w');
fwrite($fp, $url);
fclose($fp);
but received error undefined file_get_contents()

can anyone help me out?

masterpp

4:15 pm on Jun 24, 2003 (gmt 0)

10+ Year Member



i got it working now


<?php

$ch = curl_init ("http://static.php.net/www.php.net/images/php.gif");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
$rawdata=curl_exec ($ch);
curl_close ($ch);

$fp = fopen("php.gif",'w');
fwrite($fp, $rawdata);
fclose($fp);

?>

daisho

4:22 pm on Jun 24, 2003 (gmt 0)

10+ Year Member



sorry. content_type was a simple little function that I have that takes a blob and returns the Mime Type. It only works with gif and jpg's here it is:

function content_type (&$blob) {
if( strncmp($blob,"ÿØÿ",3) == 0 )
return "Content-Type: image/jpeg";
else if( strncmp($blob,"GIF",3) == 0 )
return "Content-Type: image/gif";
else
return "Content-Type: unknown";
}