Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php: forward ntlm credentials to curl

Tags:

php

curl

ntlm

I have a dynamic php page which I need to call with a get parameter. I then want to put the generated html into a string and use it later ( I'm tryign out tonic framework for web services)

So this is similar to PHP - Read dynamically generated (and echoed) HTML into a string? and I tried the answer that uses cURL.

The issue is that authentication is done with ntlm (apache mod_auth_sspi). The php script executing curl is already authenticated, eg only valid users can ever execute it. It is somehow possible to pass on these "credentials" to cURL? (username is available but of course not the password)

Or a completely different approach would be fine too but only idea I had was to make a function that creates a string with html content.

$response = new Response($request);
$format = $request->mostAcceptable(array(
    'json', 'html', 'txt'
        ));

switch ($format) {

    case 'html':
        $response->addHeader('Content-type', 'text/html');
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, 'http://localhost/viewRecord.php?identifier=' . $identifier);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_NTLM); 
        $html = curl_exec($ch);
        curl_close($ch);
        $response->body = $html;
        break;
    //...   
}
like image 903
beginner_ Avatar asked Feb 02 '12 11:02

beginner_


2 Answers

I was able to get this to work by adding the following curl options:

curl_setopt($curly[$id], CURLOPT_HTTPAUTH, CURLAUTH_NTLM);
curl_setopt($curly[$id], CURLOPT_UNRESTRICTED_AUTH, true);
curl_setopt($curly[$id], CURLOPT_USERPWD, ":");

There is a bug open for this depending on the version of php: https://bugs.php.net/bug.php?id=62195

like image 164
Rob Dawley Avatar answered Oct 01 '22 02:10

Rob Dawley


This is what worked for me:

curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_NTLM|CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_UNRESTRICTED_AUTH, true);
curl_setopt($ch, CURLOPT_USERPWD, "YOUR_USER:YOUR_PWD");
like image 33
Wolfack Avatar answered Oct 01 '22 03:10

Wolfack