phpiisauthenticationnt

How to use file_get_contents() to retrieve a file behind Windows NT Auth


I've got a setup in which a LAMP server needs to retrieve an output from a javascript file from another server IIS that is sitting behind Windows NT authentication.

Without the authentication in place, I can just use file_get_contents() to retrive the javascript file I need. However with Windows NT Authentication in place, this fails.

Does anyone have any ideas how I can simulate the authentication process in order to allow PHP to retrieve the file?


Solution

  • Use curl.

    function getUrl( $url, $username = false , $password = false ) {
      $ch = curl_init(); 
      curl_setopt($ch, CURLOPT_URL, $url); 
      curl_setopt($ch, CURLOPT_HEADER, FALSE); 
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 
    
      if( $username && $password ) {
        curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_NTLM);
        curl_setopt($ch, CURLOPT_USERPWD, "$username:$password"); 
      }
    
      $buffer = curl_exec($ch); 
      curl_close($ch); 
    
      return $buffer;
    }