使用HTTP基本身份验证从PHP发出FORM POST请求 [英] Issue FORM POST Request From PHP using HTTP Basic Authentication

查看:107
本文介绍了使用HTTP基本身份验证从PHP发出FORM POST请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望这是一个相对简单的事情,并且我的Google技能在这种情况下简直让我失望了.我有一个受BASIC身份验证保护的资源,我想让PHP对它执行POST HTTP请求.

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.

我尝试将 Authentication:Basic(加密的u/p数据)注入似乎无效的标头中-所以我想知道 Greyskull 我的意思是StackOverflow提供任何指导.

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

推荐答案

使用:

$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 之类的东西查看原始标头数据,以确保它是身份验证(您将然后也可以复制授权字符串!).

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!).

这篇关于使用HTTP基本身份验证从PHP发出FORM POST请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆