简体   繁体   English

php curl with digest返回两个响应

[英]php curl with digest returns two responses

I have spotted a "weird" php CURL behavior that is sending me nuts. 我发现了一个“奇怪的”php CURL行为让我疯了。 Basically what I am doing is making a digest authenticated call with curl. 基本上我正在做的是使用curl进行摘要认证调用。 Here's an extract of my code: 这是我的代码的摘录:

curl_setopt($this->c, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
curl_setopt($this->c, CURLOPT_USERPWD, $username . ":" . $password);

It works fine and the server actually comes back with a "YES, YOU PROVIDED THE RIGHT CREDENTIALS" kind of message. 它工作正常,服务器实际上回来了“是,你提供正确的证书”的消息。 Only trouble is, the raw http response is a bit odd as it includes, as a matter of fact, 2 responses instead of one. 唯一的麻烦是,原始的http响应有点奇怪,因为它包括,事实上,2个响应而不是一个。 Here's what curl_exec($this->c) spits out: 这是curl_exec($ this-> c)吐出的内容:

HTTP/1.0 401 Unauthorized
Date: Tue, 23 Oct 2012 08:41:18 GMT
Server: Apache/2.2.20 (Ubuntu)
X-Powered-By: PHP/5.3.6-13ubuntu3.9
WWW-Authenticate: Digest realm="dynamikrest-testing",qop="auth",nonce="5086582e95104",opaque="4b24e95490812b28b3bf139f9fbc9a66"
Vary: Accept-Encoding
Content-Length: 9
Connection: close
Content-Type: text/html

HTTP/1.1 200 OK
Date: Tue, 23 Oct 2012 08:41:18 GMT
Server: Apache/2.2.20 (Ubuntu)
X-Powered-By: PHP/5.3.6-13ubuntu3.9
Vary: Accept-Encoding
Content-Length: 9
Connection: close
Content-Type: text/html

"success"

I don't get why it includes the first response from the server (the one in which it states it requires authentication). 我不明白为什么它包含来自服务器的第一个响应(它声明它需要身份验证的响应)。

Can anyone throw some light on the issue? 任何人都可以对这个问题有所了解吗? How do I avoid the responses' cumulation? 如何避免回复累积?

Cheers 干杯

It looks like curl has the same behavior if you use the -I option for headers: 如果对标头使用-I选项,则看起来curl具有相同的行为:

curl -I  --digest -u root:somepassword http://localhost/digest-test/

returns: 收益:

HTTP/1.1 401 Authorization Required
Date: Fri, 31 May 2013 13:48:35 GMT
Server: Apache/2.2.22 (Ubuntu)
WWW-Authenticate: Digest realm="Test Page", nonce="9RUL3wPeBAA=52ef6531dcdd1de61f239ed6dd234a3288d81701", algorithm=MD5, domain="/digest-test/ http://localhost", qop="auth"
Vary: Accept-Encoding
Content-Type: text/html; charset=iso-8859-1

HTTP/1.1 200 OK
Date: Fri, 31 May 2013 13:48:35 GMT
Server: Apache/2.2.22 (Ubuntu)
Authentication-Info: rspauth="4f5f8237e9760f777255f6618c21df4c", cnonce="MTQ3NDk1", nc=00000001, qop=auth
Vary: Accept-Encoding
Content-Type: text/html;charset=UTF-8
X-Pad: avoid browser bug

To only get the second header you could try this (not very optimal solution): 要获得第二个标题,您可以尝试这个(不是非常优化的解决方案):

<?php

$ch = curl_init();
        // set url
curl_setopt($ch, CURLOPT_URL, "http://localhost/digest-test/");
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
curl_setopt($ch, CURLOPT_USERPWD, "root:test");


// first authentication with a head request
curl_setopt($ch, CURLOPT_NOBODY, 1);
curl_exec($ch);        

// the get the real output
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_HTTPGET, 1);
$output = curl_exec($ch);
echo $output;

I hit the same problem, and I think it was caused by PHP being compiled against an ancient version of libcurl (7.11.0 in my case, which is now nearly 10 years old). 我遇到了同样的问题,我认为这是因为PHP是针对古老版本的libcurl编译的(在我的情况下为7.11.0,现在已经有近10年了)。 On a different machine with a more recent version of libcurl (7.29.0) the same code was fine, and my problems ended after getting my host to recompile their PHP to use the latest they had available (7.30.0). 在具有更新版本的libcurl(7.29.0)的另一台机器上,相同的代码很好,我的问题在让我的主机重新编译他们的PHP以使用他们可用的最新版本(7.30.0)后结束。

This fix was suggested by a thread on the curl-library mailing list from 2008 , where a user discovered the problem affected version 7.10.6 but not 7.12.1. 此修复程序是由2008年curl-library邮件列表上的一个帖子提出的 ,其中用户发现问题影响版本为7.10.6而不是7.12.1。 I've searched the libcurl changelog around 7.12.0 and failed to find any clear entry about fixing this problem, though it might be covered by "general HTTP authentication improvements". 我已经搜索了7.12.0左右libcurl更改日志,但未能找到有关修复此问题的任何明确条目,尽管它可能会被“常规HTTP身份验证改进”所涵盖。 Still, I'm now pretty confident that an old libcurl is the problem. 不过,我现在非常有信心,旧的libcurl就是问题所在。

You can check which version of libcurl is used by your PHP from the 'cURL Information' entry in the output of phpinfo(); 您可以从phpinfo();输出中的'cURL Information'条目中检查PHP使用的libcurl版本phpinfo();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM