php - fsock : Unable to find the socket transport "http"

标签 php html sockets

我想用 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/

相关文章:

javascript - 如何在同一页面上使用 windows.location.href 将 js 值传递给 php 时停止刷新页面

javascript - 使用 Google javascript API 进行身份验证

html - CSS 图像不触及顶部

javascript - 解码这个动态增加输入宽度的例子

java - 为什么这个套接字为空?

c# - INET_NTOA 和 INET_ATON 的 .NET 等价物

php - 由 PHP 处理 DST 的时区

PHP 带有复选框的多重编辑

java - 普通 Java 套接字中没有密码套件

html - 如何选择 ID 以数字开头的元素?