|
|
|
Using cURL (PHP's client URL extension) to download a file programmatically.
Hi All,
Here is a simple function using cURL to download a remote file. My goal here
was to access a remote wav file hosted on another server and import it into my
system.
Steps:
1. Enable cURL (see http://ca3.php.net/manual/en/ref.curl.php).
For windows: In order to enable this module on a Windows environment, you must copy
libeay32.dll and ssleay32.dll from the DLL folder of the PHP/Win32 binary
package to the SYSTEM folder of your Windows machine.
(Ex: C:\WINNT\SYSTEM32 or C:\WINDOWS\SYSTEM)
2. Uncomment the cURL extension in the php.ini file:
extension=php_curl.dll
Functionality:
1. Its a function and accepts three parameters (you can, of course, change this)
get_file1($file, $local_path, $newfilename)
$file : is the filename of the object to be retrieved
$local_path : is the local path to the directory to store the object
$newfilename : is the new file name on the local system
2. To use it:
| <?php
$wav_file = get_file1($filename, $local_path, $newfilename);
?> | |
cURL returns true on success and false on failure.
The function:
| <?php
function get_file1($file, $local_path, $newfilename)
{
$err_msg = '';
echo "<br>Attempting message download for $file<br>";
$out = fopen($newfilename, 'wb');
if ($out == FALSE){
print "File not opened<br>";
exit;
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_FILE, $out);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_URL, $file);
curl_exec($ch);
echo "<br>Error is : ".curl_error ( $ch);
curl_close($ch);
//fclose($handle);
}//end function
?> | |
Happy coding. |
|
| Newbie Notes #8 - A cron trick Categories : PHP, CURL, Beginner Guides | | | These PHP Classes Check if a host is alive using various methods. Categories : PHP, PHP Classes, Sockets, CURL | | | Import the yahoo address book. Categories : PHP, CURL, Authentication | | | curl_close -- Close a CURL session Categories : PHP, PHP Functions, CURL | | | A function to check if a URL exists Categories : PHP, CURL, HTTP | | | Yahoo! Messenger Friend List Categories : PHP, CURL, HTML and PHP | | | Audio-Enable Discussion Boards with this Web-page Audio Recorder / Player! Categories : PHP Extensions, Java, PHP | | | Amazon.com API, CURL-REST Parser. Obtain data about Amazon products (PHP5 +) Categories : PHP, Ecommerce, XML, Web Services, CURL | | | PHP Youtube Downloader - This is a set of PHP functions that can be used to download movies from Youtube.com.
Categories : PHP, CURL, Regexps | | | Returns Yahoo! Address Book and Messenger List as an Array Categories : PHP, PHP Classes, CURL | | | Query2Report : Generating Html, Pdf and Csv Reports from SQL Query Categories : PHP, PHP, HTML, PDF, Excel | | | Open remote https url using Curl and accept cookie Categories : PHP, CURL | | | Sample AIM (Advanced Integration Method) PHP Script For Authorize.net using CURL Categories : CURL, Ecommerce, PHP | | | Link Manager for Link Exchangers Categories : PHP, PHP Classes, Databases, MySQL, CURL | | | Using data from a string. Categories : PHP, Strings, CURL | |
|
|
|