简体   繁体   English

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

[英]Issue FORM POST Request From PHP using HTTP Basic Authentication

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. 我有一个BASIC身份验证保护资源,我想让PHP执行POST HTTP请求。

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. 我已经尝试将身份验证:基本(加密的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);
}

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!). 使用像CharlesProxy这样的东西来查看原始标题数据可能是值得的,以确保它是一个身份验证(然后你也可以复制授权字符串!)。

Here's a function I use to perform POST requests. 这是我用来执行POST请求的函数。 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 以下是我使用它将POST变量转发到API的示例

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM