【问题标题】:PHP - with curl, how to send username password in a popup of htpasswd?PHP - 使用 curl,如何在 htpasswd 的弹出窗口中发送用户名密码?
【发布时间】:2016-07-07 06:07:18
【问题描述】:

我必须使用 curl 提交 POST 方法,但是在打开 $URL 时,它会弹出一个带有要提交的用户名/密码的窗口(就像使用 htpasswd 一样)。

由于某种原因,我从服务器获取的以下代码不起作用

HTTP/1.1 401 Unauthorized and HTTP/1.1 404 Not Found (https://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;

【问题讨论】:

    标签: php apache authentication post curl


    【解决方案1】:

    使用 HTTP Auth,您可以将用户名和密码作为 URL 的一部分发送:

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

    另见this SO question and answer

    但是,CURLOPT_USERPWD 应该和这两种方法一样只设置 HTTP Authorization 标头。 as mentioned here。这也更安全,因为您的凭据不会作为 URL 中的纯文本传输,并且可能会使用 SSL/TLS,因此它们将不会公开显示。

    另外,设置 HTTP 基本身份验证可能会有所帮助。

    curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
    

    【讨论】:

    • 那么,你是说我的代码应该删除这一行吗? curl_setopt($ch, CURLOPT_USERPWD, "$username:$password"); 和 $URL 我应该添加 $username:$password@$real_url ?
    • @YumYumYum 完全正确。
    • 我的原始代码和您建议的代码。仍然做完全相同的结果。请参阅我上面的编辑部分
    • 您必须在 http:// 架构之前放置凭据和 @。也使用=,而不是.=$URL = "$username:$password@$URL";
    • 是的 - 我已经解决了这个问题,但还是和以前一样,还没有运气。
    猜你喜欢
    • 2017-09-12
    • 1970-01-01
    • 1970-01-01
    • 2013-09-10
    • 1970-01-01
    • 2022-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多