PHP CURL 不返回任何内容
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6324819/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP CURL returns nothing
提问by jM2.me
function ParseUrl($URL)
{
$crl = curl_init();
curl_setopt ($crl, CURLOPT_URL, $URL);
curl_setopt ($crl, CURLOPT_PORT, 8086);
curl_setopt ($crl, CURLOPT_USERPWD, "admin:pass");
curl_setopt ($crl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($crl, CURLOPT_CONNECTTIMEOUT, 5);
$ret = curl_exec($crl);
curl_close($crl);
return $ret;
}
echo ParseUrl('http://xxx.me/serverinfo');
The code above simply returns nothing. The page I am trying to get with curl uses http authentication thing.
上面的代码只是不返回任何内容。我试图通过 curl 获取的页面使用了 http 身份验证。
Am I missing something simple or what?
我错过了一些简单的东西还是什么?
回答by rzetterberg
Start out by doing this and see what you get, and after that it would be pretty obvious what the problem is:
从这样做开始,看看你得到了什么,然后很明显问题是什么:
Check if there was an error with the request after curl_exec:
在 curl_exec 之后检查请求是否有错误:
if(curl_errno($ch)){
echo 'Curl error: ' . curl_error($ch);
}
That will provide you with enough info to know if there was a error with the request. If there was no error, you can check the request sent after curl_exec so you can double check that everything sent is in place:
这将为您提供足够的信息来了解请求是否有错误。如果没有错误,您可以检查 curl_exec 之后发送的请求,以便您可以仔细检查发送的所有内容是否到位:
print_r(curl_getinfo($ch));
Edit: After comments this is what you are looking for what is missing:
编辑:评论后,这就是您要查找的内容:
You need to set the option CURLOPT_HTTPAUTH
.
您需要设置选项CURLOPT_HTTPAUTH
。
Quote from php.net on the subject:
The HTTP authentication method(s) to use. The options are: CURLAUTH_BASIC, CURLAUTH_DIGEST, CURLAUTH_GSSNEGOTIATE, CURLAUTH_NTLM, CURLAUTH_ANY, and CURLAUTH_ANYSAFE.
The bitwise | (or) operator can be used to combine more than one method. If this is done, cURL will poll the server to see what methods it supports and pick the best one.
CURLAUTH_ANY is an alias for CURLAUTH_BASIC | CURLAUTH_DIGEST | CURLAUTH_GSSNEGOTIATE | CURLAUTH_NTLM.
CURLAUTH_ANYSAFE is an alias for CURLAUTH_DIGEST | CURLAUTH_GSSNEGOTIATE | CURLAUTH_NTLM.
要使用的 HTTP 身份验证方法。选项包括:CURLAUTH_BASIC、CURLAUTH_DIGEST、CURLAUTH_GSSNEGOTIATE、CURLAUTH_NTLM、CURLAUTH_ANY 和 CURLAUTH_ANYSAFE。
按位 | (or) 运算符可用于组合多个方法。如果这样做,cURL 将轮询服务器以查看它支持哪些方法并选择最好的方法。
CURLAUTH_ANY 是 CURLAUTH_BASIC 的别名 | CURLAUTH_DIGEST | CURLAUTH_GSSNEGOTIATE | CURLAUTH_NTLM。
CURLAUTH_ANYSAFE 是 CURLAUTH_DIGEST 的别名 | CURLAUTH_GSSNEGOTIATE | CURLAUTH_NTLM。