Hello there :)
I´m playing around with cURL to learn some new things.
Anyway i just wanted to share a simple code snippet i came up with when playing around. Nothing revolutionary but handy.
I´t will ofcourse need optimization to fitt your individual needs.
But in my case i was looking for a way to "download" image files to my server and store them there as files.
And i could not use URL wrapers with the conventional ways because of secrity measures by my webhost. So i thought why not try to use cURL and to my suprise the code worked. And this code is just barebone.
You are welcome to leave some tips on improvement on this code.
Here goes nothing:
PHP Code:
<?PHP
// Set Target URL
$ch = curl_init('http://www.talkphp.com/images/talkphp/talkphp_logo.jpg');
// Set cURL options
curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1);
curl_setopt($ch, CURLOPT_FORBID_REUSE, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// Change to TRUE for BINARY transfer
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
// in $result is our string with the return data
// we also execute the cURL
$result = curl_exec($ch);
curl_close($ch); // Closing connection
// create a file if note present for read/write
$handle = fopen('random_img.jpg', 'w+');
// save data to a file on server
fwrite($handle, $result);
// close the file
fclose($handle);
// see if the file works ?
echo('<IMG SRC="random_img.jpg" />');
?>
Remember my webhost does currently not use PHP5 so i could not use any of thoose CURLOPT_xxx options.
Should one use the BINARY option set to TRUE if your retrieving text files like HTML, PHP, XML and so on ?
Use my code improve it, or whatever you like :)
Good Luck!
/EyeDentify