2013-05-12 59 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(); 
    } 
}