2013-05-12 62 views
2

我想用密码保护我的域名,因为我只是用它来测试脚本,但我需要允许curl HTTP POST请求。这可能吗?.htaccess密码验证,但允许发布请求

当前的.htaccess:

#AuthUserFile /home/domain/.htpasswds/.htpasswd 
#AuthType Basic 
#AuthName "Protected Domain" 
#Require valid-user 

PHP卷曲请求

$handle = curl_init('http://wwww.domain.com/cgi/mailScript.php'); 
    curl_setopt($handle, CURLOPT_POST, 1); 
    curl_setopt($handle, CURLOPT_POSTFIELDS, $serial); 
    curl_exec($handle); 

PHP错误:

Authorization Required 

This server could not verify that you are authorized to access the document requested. Either you supplied the wrong credentials (e.g., bad password), or your browser doesn't understand how to supply the credentials required. 

Additionally, a 401 Authorization Required error was encountered while trying to use an ErrorDocument to handle the request. 

编辑:我不担心安全问题尽可能只是防止人从探索该网站。

+0

你为什么不通过curl请求登录? – SLaks 2013-05-12 15:05:47

+0

这甚至没有发生在我身上,我会研究这一点。 – chuckieDub 2013-05-12 15:07:48

回答

4
$username = 'login'; 
$password = 'pass'; 
//... 
curl_setopt($handle, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); 
curl_setopt($handle, CURLOPT_USERPWD, $username . ":" . $password); 
curl_exec($handle); 
1

这是一个如何实现这个功能的示例函数。

function curl_bounce_server($url, $param) 
{ 
    try 
    { 
     $ch = curl_init(); 

     $username = "your_user"; 
     $password = "your_pass"; 

     // set URL and other appropriate options 
     curl_setopt($ch, CURLOPT_URL, $url); 
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
     curl_setopt($ch, CURLOPT_POST,   1); 
     curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60); 
     curl_setopt($ch, CURLOPT_TIMEOUT, 30); 

     curl_setopt($ch, CURLOPT_USERPWD, "$username:$password"); 
     curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); 

     curl_setopt($ch, CURLOPT_POSTFIELDS, 'json='. $param); 

     $response = curl_exec($ch); 

     curl_close($ch); 

     return $response; 
    } 
    catch (Exception $exception) 
    { 
     return "An exception occurred when executing the server call - exception:" . $exception->getMessage(); 
    } 
}