我想用 fsock 发送 post 变量, 但是当我尝试这个时:
$post_arr = array ("a" => "b");
$addr = 'http://1.2.3.4/confirmation.html';
$fp = fsockopen($addr, 80, $errno, $errstr, 30);
if (!$fp) {
echo "$errstr ($errno)<br />\n";
} else {
$req = '';
foreach ($post_arr as $key => $value) {
$value = urlencode(stripslashes($value));
$req .= "&" . $key . "=" . $value;
}
$header = "POST /cgi-bin/webscr HTTP/1.0\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: " . strlen($req) . "\r\n\r\n";
fwrite($fp, $header);
while (!feof($fp)) {
echo fgets($fp, 128);
}
fclose($fp);
}
我得到“无法找到套接字传输“http””, 有什么想法吗?
最佳答案
fsockopen()
打开套接字。套接字对 HTTP 等 Layer5+ 协议(protocol)一无所知。
$fp = fsockopen('1.2.3.4', 80, $errno, $errstr, 30);
要请求某个路径,在请求中发送:GET/confirmation.html
要指定域,请在 Host
header 中发送:Host: 1.2.3.4
您可能需要考虑使用 curl
扩展。通常没有充分的理由在 PHP 中手动构建 HTTP 请求。
关于php - fsock : Unable to find the socket transport "http",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9965430/