Page 1 of 1

getting images through curl execution

Posted: Wed Mar 25, 2009 5:36 am
by susrisha
I have been trying to get this thing working for a while.

I have a link
http://www.makayama.com/android/vara.png

Now i need a curl execution on a page on my local server which will get the png content and display it back to the user.

So for example, if the user types http://www.mysite.com/mypage.php, he/she should be able to view the above image

redirect is also another idea but i need the content to be displayed hiding the actual url.

Have done the following but i get the values as junk. This same code works if i try to get an xml file from a location.

Code: Select all

 
 
 
 
$URL = 'http://www.makayama.com/android/vara.png';
//initiate the curl 
$ch = curl_init();
//set the URL to the curl handle
curl_setopt($ch, CURLOPT_URL, $URL);
//ignore header
curl_setopt($ch, CURLOPT_HEADER, false);
 
curl_setopt($ch,CURLOPT_BINARYTRANSFER,true);
 
 
// grab URL and pass it to the browser
curl_exec($ch);
 
// close CURL resource, and free up system resources
 
curl_close($ch);
 

Re: getting images through curl execution

Posted: Wed Mar 25, 2009 5:56 am
by Benjamin

Code: Select all

 
$URL = 'http://www.makayama.com/android/vara.png';
//initiate the curl 
$ch = curl_init();
//set the URL to the curl handle
curl_setopt($ch, CURLOPT_URL, $URL);
//ignore header
curl_setopt($ch, CURLOPT_HEADER, false);
 
curl_setopt($ch,CURLOPT_BINARYTRANSFER,true);
 
# TRUE to return the transfer as a string of the return value of curl_exec() instead of outputting it out directly.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 
// grab URL and pass it to the browser
$image = curl_exec($ch);
 
// close CURL resource, and free up system resources
curl_close($ch);
 
header('Last-Modified: ' . date('r'));
header('Accept-Ranges: bytes');
header('Content-Length: ' . strlen($image));
header('Content-Type: image/png');
echo $image;
 
 

Re: getting images through curl execution

Posted: Wed Mar 25, 2009 6:24 am
by susrisha
:drunk:

Thanks a lot astions for that.. Have been :banghead: for a while.

Can i use the same code for other picture formats by changing this line?

Code: Select all

 
header('Content-Type: image/png');
 

Re: getting images through curl execution

Posted: Wed Mar 25, 2009 2:32 pm
by Benjamin
Yeah, that's fine :)