[英]Issue FORM POST Request From PHP using HTTP 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);
}
使用:
$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这样的东西来查看原始标题数据可能是值得的,以确保它是一个身份验证(然后你也可以复制授权字符串!)。
这是我用来执行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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.