1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5.00 out of 5)
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. #1 by Anthony Scaife on January 13th, 2012

    Thanks. Just what I was looking for. I used it to get at the RSS for my Google bookmarks:

  2. #2 by Anthony Scaife on January 13th, 2012

    Sorry, In my last post I included PHP tags. It looks to have broken the comment. So . . .

    Thanks. Just what I was looking for. I used it to get at the RSS for my Google bookmarks:

    $curlObj = curl_init();
    curl_setopt($curlObj, CURLOPT_URL, “https://www.google.com/bookmarks/?output=rss”);
    curl_setopt($curlObj, CURLOPT_USERPWD, “my_account_login_email:my_account_login_password”);
    curl_setopt ($curlObj, CURLOPT_RETURNTRANSFER, 1);
    $response = curl_exec($curlObj);
    echo $response;
    curl_close($curlObj);

(will not be published)
Security Code:

  1. No trackbacks yet.