Skip to content
PHP Logo

How can a WWW-Authenticate with NTLM be implemented in PHP?

To implement a WWW-Authenticate with NTLM in PHP, you can use the cURL library to make an HTTP request with the appropriate headers. This involves setting the CURLOPT_HTTPAUTH option to CURLAUTH_NTLM and providing the username, password, and domain in the CURLOPT_USERPWD option. Additionally, you may need to handle the NTLM challenge/response mechanism to authenticate successfully.

php
<?php

$username = 'your_username';
$password = 'your_password';
$domain = 'your_domain';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://example.com/api');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_NTLM);
curl_setopt($ch, CURLOPT_USERPWD, "$domain\\$username:$password");

$response = curl_exec($ch);

if($response === false){
    echo 'Error: ' . curl_error($ch);
} else {
    echo $response;
}

curl_close($ch);

?>