1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...

Access files in password protected directory using curl


I had to get a page that is in a password protected directory.
First I used php file_get_contents() which is always my first solution for accessing remote pages. But many web servers disable allow_url_fopen in php config. So curl is the ultimate solution.

Curl has an option called CURLOPT_USERPWD that allows to add the user name and password along with the request. A sample code may be as following:


<?php
$curlObj = curl_init();
curl_setopt($curlObj, CURLOPT_URL, "http://domain/passdprotected/filename");
curl_setopt($curlObj, CURLOPT_USERPWD, "username:password");
curl_exec($curlObj);
curl_close($curlObj);
?>

Hope this helps!!

Bookmark and Share

, , ,

  1. No comments yet.
(will not be published)
Security Code:

  1. No trackbacks yet.