使用HTTP基本身份验证从PHP发出FORM POST请求

Dav*_*sen 10 php post basic-authentication

我希望这是一个相对直接的事情,而且我的谷歌技能在这个场合让我失望了.我有一个BASIC身份验证保护资源,我想让PHP执行POST HTTP请求.

我已经尝试将身份验证:基本(加密的u/p数据)注入到看起来不起作用的标题中 - 所以我想知道,Greyskull的功能是否可以表示StackOverflow提供任何指导.

$req .= "&cmd=_initiate_query";
$header = "POST /someendpoint HTTP/1.1\r\n".
        "Host:example.com\n".
        "Content-Type: application/x-www-form-urlencoded\r\n".
        "User-Agent: PHP-Code\r\n".
        "Content-Length: " . strlen($req) . "\r\n".
        "Connection: close\r\n\r\n";
$fp = fsockopen ('ssl://example.com', 443, $errno, $errstr, 30);
if (!$fp) {
    // HTTP ERROR
} else {
    fputs ($fp, $header . $req);
    while (!feof($fp)) {
        $result .= fgets ($fp, 128);
    }
    fclose ($fp);
}
Run Code Online (Sandbox Code Playgroud)

Ric*_* B. 4

使用:

$header = "POST /someendpoint HTTP/1.1\r\n".
        "Host:example.com\n".
        "Content-Type: application/x-www-form-urlencoded\r\n".
        "User-Agent: PHP-Code\r\n".
        "Content-Length: " . strlen($req) . "\r\n".
        "Authorization: Basic ".base64_encode($username.':'.$password)."\r\n".
        "Connection: close\r\n\r\n";
Run Code Online (Sandbox Code Playgroud)

应该可以工作 - 您确定它是基本身份验证系统吗?可能值得使用CharlesProxy之类的东西来观察原始标头数据,以确保它是身份验证(然后您也可以复制授权字符串!)。