How to use file_get_contents() to retrieve

2019-07-02 08:01发布

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?

1条回答
迷人小祖宗
2楼-- · 2019-07-02 08:18

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;
}
查看更多
登录 后发表回答