PHP CURL 不返回任何内容
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');
上面的代码根本不返回任何内容。我试图使用curl 获取的页面使用http 身份验证。
我错过了一些简单的事情还是什么?
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.
Am I missing something simple or what?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先这样做,看看你得到了什么,然后问题就很明显了:
检查curl_exec之后的请求是否有错误:
这将为你提供足够的信息来知道是否有错误与请求。如果没有错误,您可以检查在curl_exec之后发送的请求,以便您可以仔细检查发送的所有内容是否就位:
编辑:在注释之后,这就是您正在寻找的缺少的内容:
您需要设置选项
CURLOPT_HTTPAUTH
。引用自 php.net 关于该主题的内容:
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:
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:
Edit: After comments this is what you are looking for what is missing:
You need to set the option
CURLOPT_HTTPAUTH
.Quote from php.net on the subject:
就我而言,端点定义之前有一个空格。
因此,如果curl 响应没有返回任何内容,请在疯狂之前仔细检查您定义的curl 选项,无论发布的json 是否有效。
In my case there was a space before endpoint definition.
So if nothing returns from a curl response, double check the curl options you define before going insane whether the posted json is valid or not.