簡體   English   中英

PHP-帶有curl,如何在htpasswd的彈出窗口中發送用戶名密碼?

[英]PHP - with curl, how to send username password in a popup of htpasswd?

我必須使用curl提交POST方法,但是在打開$ URL時,它會彈出一個帶有用戶名/密碼的提交窗口(例如htpasswd)。

由於某些原因,以下代碼無法正常工作,我從服務器獲取

HTTP/1.1 401 Unauthorized and HTTP/1.1 404 Not Foundhttps://www.httpwatch.com/httpgallery/authentication/

// For Debug server response details
$curlOptions = array(
    CURLOPT_RETURNTRANSFER => TRUE,
    CURLOPT_FOLLOWLOCATION => TRUE,
    CURLOPT_VERBOSE => TRUE,
    CURLOPT_STDERR => $verbose = fopen('php://temp', 'rw+'),
    CURLOPT_FILETIME => TRUE,
);

// Real meat
$ch = curl_init();
curl_setopt_array($ch, $curlOptions);
curl_setopt($ch, CURLOPT_URL,$URL);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);

// is this the right way here in POST method???
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");// is this correct way to enter username/password in the popup?

$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$result=curl_exec ($ch);

// Pretty print the debug logs
echo '<pre>', 
       !rewind($verbose), 
       stream_get_contents($verbose), 
       "\n", 
     '</pre>';

curl_close ($ch);
echo $result;

我如何確定我的POST方法提交確實是在提交用戶名/密碼? (調試日志我看不到我是否已正確提交,服務器也不由我維護,因為它是第三方服務提供商的API)

編輯:

TRY1:失敗

// For Debug server response details
$curlOptions = array(
    CURLOPT_RETURNTRANSFER => TRUE,
    CURLOPT_FOLLOWLOCATION => TRUE,
    CURLOPT_VERBOSE => TRUE,
    CURLOPT_STDERR => $verbose = fopen('php://temp', 'rw+'),
    CURLOPT_FILETIME => TRUE,
);

// Real meat
$ch = curl_init();
curl_setopt_array($ch, $curlOptions);


//
//
//
//
// STACK-OVERFLOW EXPERT SAID UPDATE THE $URL WITH USER:PASS@ THIS
//
// 
//  
//    

$URL = "http://$username:$password@site.com/postblabla";
curl_setopt($ch, CURLOPT_URL, $URL);



curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);


//
//
//
//
// STACK-OVERFLOW EXPERT SAID REMOVE THIS
//
// 
//  
//    
//curl_setopt($ch, CURLOPT_USERPWD, "$username:$password"); 

$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$result=curl_exec ($ch);

// Pretty print the debug logs
echo '<pre>', 
       !rewind($verbose), 
       stream_get_contents($verbose), 
       "\n", 
     '</pre>';

curl_close ($ch);
echo $result;

使用HTTP Auth,您可以發送用戶名和密碼作為URL的一部分:

<schema>://<username>:<password>@<url>

另請參閱此SO問答

但是, CURLOPT_USERPWD應該可以正常工作,並且這兩種方法都只能設置HTTP Authorization標頭。 如這里提到的 這也更安全,因為您的憑據將不會以純文本格式在URL中傳輸,並且可能會使用SSL / TLS,因此不會在公開位置顯示。

另外,設置HTTP基本身份驗證可能會有所幫助。

curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM