PHP, cURL, HTTP PUT, SSL and Basic Authentication
January 2nd, 2006
Here's a nice script I just put together for placing files on DAV-enabled web space, catering for SSL and HTTP authentication. It'll even log the result as presented from an Apache + mod_dav server.
Enjoy!
PHP:
-
// Initialise cURL session
-
$curl = curl_init();
-
-
// Specify the username/password to use, or leave blank for no auth
-
$user = "username";
-
$password = "password";
-
-
// load in the file to process and extract the filesize and filename
-
$filename = "./path/to/file.txt";
-
-
// The DAV-enabled URL to PUT to, appended with the destination filename
-
$url = "https://www.example.com/dav-folder/{$destinationFilename}";
-
-
// Set cURL parameters for this transaction
-
-
// Load in the destination URL
-
curl_setopt($curl,CURLOPT_URL,$url);
-
-
// tell cURL we're doing a PUT
-
curl_setopt($curl,CURLOPT_PUT,true);
-
-
// present the filesize of the file we're putting
-
curl_setopt($curl,CURLOPT_INFILESIZE,$size);
-
-
// load the file in by its resource handle
-
curl_setopt($curl,CURLOPT_INFILE,$file);
-
-
// Place a nice friendly user-agent
-
curl_setopt($curl,CURLOPT_USERAGENT,"Mozilla/4.0");
-
-
// return the output instead of displaying it
-
curl_setopt($curl,CURLOPT_RETURNTRANSFER,true);
-
-
// provide credentials if they're established at the beginning of the script
-
curl_setopt($curl,CURLOPT_USERPWD,$user . ":" . $password);
-
-
// tell cURL to graciously accept an SSL certificate if presented
-
curl_setopt($curl,CURLOPT_SSL_VERIFYPEER,false);
-
-
// execute, and log the result to curl_put.log
-
$result = curl_exec($curl);
-
$error = curl_error($curl);
-
else {
-
$return = "Error";
-
$message = curl_error();
-
}
-
-
-
-
// Tidy up!
-
curl_close($curl);
Add to del.icio.us
Digg this
Technorati
Related Posts:
4 Comments Add your own
1. Determining if a URL exis&hellip | June 9th, 2006 at 9:34 am
[...] Curl is a collection of client URL library functions that we can use to perform a multitude of URL related activities. You can send a POST or file upload to a URL, use it as a proxy, send/receive XML requests, and much more. In fact, there are over 100 configuration options that can customise this to your needs. [...]
2. Charles | August 17th, 2006 at 6:36 pm
I am trying to use cURL to access a file that is under a folder that is password protected using https.
How would I got about doing this? I tried to use the code above but I don't quite know what I am doing with cURL. The file is a .php script that does some things for me.
3. Christopher Vrooman | March 17th, 2008 at 11:15 pm
Maybe a dumb question, but let's say I want to send a cURL encoded username:password to my server. How do I then retrieve that information so I can verify it?
I've seen a hundred tutorials on how to send information the username and password via cURL, but not one on how to deal with that from the server side.
I tried var_dump'ing $GLOBALS and the information isn't in any of PHP's superglobal arrays. Any clues on how to receive the information that cURL is sending?
4. tom | December 15th, 2008 at 9:45 pm
Christopher, I have been trying to figure out the same thing! It has to be real simple since there is nothing out there showing what to do.
Leave a Comment
Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>
Trackback this post | Subscribe to the comments via RSS Feed