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:
  1. // Initialise cURL session
  2. $curl = curl_init();
  3.  
  4. // Specify the username/password to use, or leave blank for no auth
  5. $user = "username";
  6. $password = "password";
  7.  
  8. // load in the file to process and extract the filesize and filename
  9. $filename = "./path/to/file.txt";
  10. $size = filesize($filename);
  11. $file = fopen($filename,'r');
  12. list(,$destinationFilename) = pathinfo($filename);
  13.  
  14. // The DAV-enabled URL to PUT to, appended with the destination filename
  15. $url = "https://www.example.com/dav-folder/{$destinationFilename}";
  16.  
  17. // Set cURL parameters for this transaction
  18.  
  19. // Load in the destination URL
  20. curl_setopt($curl,CURLOPT_URL,$url);
  21.  
  22. // tell cURL we're doing a PUT
  23. curl_setopt($curl,CURLOPT_PUT,true);
  24.  
  25. // present the filesize of the file we're putting
  26. curl_setopt($curl,CURLOPT_INFILESIZE,$size);
  27.  
  28. // load the file in by its resource handle
  29. curl_setopt($curl,CURLOPT_INFILE,$file);           
  30.  
  31. // Place a nice friendly user-agent
  32. curl_setopt($curl,CURLOPT_USERAGENT,"Mozilla/4.0");
  33.  
  34. // return the output instead of displaying it
  35. curl_setopt($curl,CURLOPT_RETURNTRANSFER,true);
  36.  
  37. // provide credentials if they're established at the beginning of the script
  38. if(!empty($user) && !empty($password))
  39.     curl_setopt($curl,CURLOPT_USERPWD,$user . ":" . $password);
  40.  
  41. // tell cURL to graciously accept an SSL certificate if presented
  42. if(ereg("^(https)",$url))
  43.     curl_setopt($curl,CURLOPT_SSL_VERIFYPEER,false);
  44.  
  45. // execute, and log the result to curl_put.log
  46. $result = curl_exec($curl);
  47. $error = curl_error($curl);
  48. if(empty($error))
  49.     list($return,,$message) = split("(\r?\n){1,}",trim(strip_tags($result)));
  50. else {
  51.     $return = "Error";
  52.     $message = curl_error();
  53. }
  54.  
  55. $log = fopen('curl_put.log','a');
  56.  
  57. fwrite($log,date("r - ") . $return . "\r\n" . $message . "\r\n\r\n");
  58.  
  59. // Tidy up!
  60. fclose($log);
  61. curl_close($curl);


 Add to del.icio.us    Digg this    Technorati

Related Posts:

Entry Filed under: PHP, Apache

3 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?

Leave a Comment

Required

Required, hidden

Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>

Trackback this post  |  Subscribe to the comments via RSS Feed


Calendar

January 2006
M T W T F S S
« Dec   Feb »
 1
2345678
9101112131415
16171819202122
23242526272829
3031  

Most Recent Posts