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

发布于 2024-07-29 16:53:04 字数 767 浏览 4 评论 0原文

我希望这是一件相对简单的事情,我的谷歌技能这次让我失败了。 我有一个受 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);
}

I hope this is a relatively straight forward thing to do, and that my google skills have simply failed me on this occasion. I have a BASIC authentication protected resource which I want to have PHP perform a POST HTTP request against.

I have tried injecting Authentication: Basic (encrypted u/p data) into the headers which didn't appear to work - so I wonder, can the powers of Greyskull i mean StackOverflow provide any guidance.

$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);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

谜泪 2024-08-05 16:53:04

使用:

$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";

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

Using:

$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";

Should work - are you sure it is a Basic authentication system? It may be worth watching the raw header data using something like CharlesProxy to ensure it is an authentication (and you'll be then able to copy the authorization string as well!).

固执像三岁 2024-08-05 16:53:04

这是我用来执行 POST 请求的函数。 希望它可以实现您想要的功能:

function http_post($server, $port, $url, $vars) {

// get urlencoded vesion of $vars array 
$urlencoded = ""; 

foreach ($vars as $Index => $Value) 
    $urlencoded .= urlencode($Index ) . "=" . urlencode($Value) . "&"; 

$urlencoded = substr($urlencoded,0,-1);  

$headers = "POST $url HTTP/1.0\r\n" 
. "Content-Type: application/x-www-form-urlencoded\r\n" 
. "Content-Length: ". strlen($urlencoded) . "\r\n\r\n"; 

$fp = fsockopen($server, $port, $errno, $errstr, 10); 
if (!$fp) return "ERROR: fsockopen failed.\r\nError no: $errno - $errstr"; 

fputs($fp, $headers); 
fputs($fp, $urlencoded); 

$ret = ""; 
while (!feof($fp)) $ret .= fgets($fp, 1024); 

fclose($fp); 
return $ret; }

这是我使用它将 POST 变量转发到 API 的示例

$response = http_post("www.nochex.com", 80, "/nochex.dll/apc/apc", $_POST); 

Here's a function I use to perform POST requests. Hopefully it can do what you want:

function http_post($server, $port, $url, $vars) {

// get urlencoded vesion of $vars array 
$urlencoded = ""; 

foreach ($vars as $Index => $Value) 
    $urlencoded .= urlencode($Index ) . "=" . urlencode($Value) . "&"; 

$urlencoded = substr($urlencoded,0,-1);  

$headers = "POST $url HTTP/1.0\r\n" 
. "Content-Type: application/x-www-form-urlencoded\r\n" 
. "Content-Length: ". strlen($urlencoded) . "\r\n\r\n"; 

$fp = fsockopen($server, $port, $errno, $errstr, 10); 
if (!$fp) return "ERROR: fsockopen failed.\r\nError no: $errno - $errstr"; 

fputs($fp, $headers); 
fputs($fp, $urlencoded); 

$ret = ""; 
while (!feof($fp)) $ret .= fgets($fp, 1024); 

fclose($fp); 
return $ret; }

And here's an example of me using it to forward on the POST variables to an API

$response = http_post("www.nochex.com", 80, "/nochex.dll/apc/apc", $_POST); 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文