3

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?

Mezzair
  • 317
  • 5
  • 12

1 Answers1

8

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;
}
mobius
  • 5,104
  • 2
  • 28
  • 41
  • When using cURL, I think it is wise to set `CURLOPT_TIMEOUT` and `CURLOPT_CONNECTTIMEOUT` just in case. One of the perks of using cURL. – danielrsmith Nov 14 '11 at 13:10
  • Awesome, worked perfect. Just had to change one line from `curl_setopt($curl, CURLOPT_USERPWD, "$username:$password");` to `curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");` – Mezzair Nov 14 '11 at 13:11
  • Oh yeah, typo. Sorry I've fixed that :) – mobius Nov 14 '11 at 13:13