需要帮助libcurl用户名/密码认证 [英] Need help with libcurl username/password authentication
问题描述
我正在尝试访问我们的某个服务器上的安全网页。该网页位于.htaccess受保护的目录中,需要用户名和密码。我试图让libcurl提交身份验证请求,以便它可以访问该文件。在各个地方找到一些指导后,我想出了我相信应该工作,但它不是。
I'm trying to access a secure webpage on one of our servers. The webpage is inside a .htaccess protected directory that requires a username and password. I'm trying to get libcurl to submit the authentication request so it can access the file. After finding some guidance in various places, I've come up with what I believe should work, but it doesn't.
$url="https://www.myurl.com/htaccess_secured_directory/file.php";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, 'myusername:mypassword');
$result = curl_exec($ch);
echo $result;
file.php文件应该只是回显Hello World!空白。有没有办法从curl返回错误信息?我做错了什么?我也使用CURLLAUTH_ALL没有成功,但我相信我只是有基本的授权。
The file.php file should simply echo "Hello World!", but the $result variable is coming up blank. Is there a way to get error messages back from curl? Am I doing something wrong? I've also used CURLLAUTH_ALL without success, but I believe I just have basic authorization anyway.
推荐答案
使用 curl_errno()
和 curl_error ()
以检索错误信息,即:
Use curl_errno()
and curl_error()
to retrieve error information, ie:
$url="https://www.myurl.com/htaccess_secured_directory/file.php";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, 'myusername:mypassword');
$result = curl_exec($ch);
if(curl_errno($ch))
{
echo 'Curl error: ' . curl_error($ch);
}
else
{
echo $result;
}
curl_close($ch);
可用的错误代码记录在此处。
The available error codes are documented here.
这篇关于需要帮助libcurl用户名/密码认证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!