|
|
|
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. |
|
| Audio-Enable Discussion Boards with this Web-page Audio Recorder / Player! Categories : PHP Extensions, Java, PHP | | | PHP Youtube Downloader - This is a set of PHP functions that can be used to download movies from Youtube.com.
Categories : PHP, CURL, Regexps | | | A function to check if a URL exists Categories : PHP, CURL, HTTP | | | Returns Yahoo! Address Book and Messenger List as an Array Categories : PHP, PHP Classes, 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 | | | 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 | | | Amazon.com API, CURL-REST Parser. Obtain data about Amazon products (PHP5 +) Categories : PHP, Ecommerce, XML, Web Services, CURL | | | 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 | | | Using data from a string. Categories : PHP, Strings, CURL | | | Url To Pdf Report By Remote Application Categories : PHP, PHP Classes, PDF, CURL | | | Newbie Notes #8 - A cron trick Categories : PHP, CURL, Beginner Guides | | | curl_close -- Close a CURL session Categories : PHP, PHP Functions, CURL | |
| | | | Mark Dhas wrote :1812
I don't believe this code actually uses the localpath
variable, so by default this file should save to the cwd
(current working directory).
To save the file to the directory of your choosing change
the following line
<?php
$out = fopen($newfilename,"wb");
?>
to
<?php
$out = fopen($localpath.$newfilename,"wb");
?>
| |
|
|