ixudra / curl

Custom PHP curl library for the Laravel 5 framework - developed by Ixudra
MIT License
561 stars 128 forks source link

Curl doesn't work to authenticate #96

Closed RosiersRobin closed 6 years ago

RosiersRobin commented 6 years ago

So I have written this code

$response = Curl::to($url)->withOption('USERPWD', $username . ":" . $password)
                                    ->withOption('HTTPAUTH', 'CURLAUTH_DIGEST')
                                    ->withOption('RETURNTRANSFER', 1)
                                    ->withOption('FOLLOWLOCATION', 1)
                                    ->get();

And it throws the error that I'm not authenticated... Now the "bare" Curl code is the following:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$output = curl_exec($ch);
curl_close($ch);
return $output;

What am I doing wrong here?

RosiersRobin commented 6 years ago

It needs Digest authentication

elimentz commented 6 years ago

Hi Robin

In the "base" curl request, you use the CURLAUTH_DIGEST constant, whereas in the ixudra/curl request, you use a string which contains the value 'CURLAUTH_DIGEST'. These are two completely different things. Try again like this:

$response = Curl::to($url)
    ->withOption('USERPWD', $username . ":" . $password)
    ->withOption('HTTPAUTH', CURLAUTH_DIGEST)
    ->withOption('RETURNTRANSFER', 1)
    ->withOption('FOLLOWLOCATION', 1)
    ->get();

You can simplify even more like this:

$response = Curl::to($url)
    ->withOption('USERPWD', $username . ":" . $password)
    ->withOption('HTTPAUTH', CURLAUTH_DIGEST)
    ->allowRedirect()
    ->get();

since CURLOPT_RETURNTRANSFER is set to true by default and the allowRedirect() method is basically a more readable way to set the CURLOPT_FOLLOWLOCATION

Hope this helps.

elimentz commented 6 years ago

Closed due to inactivity