Php – fsock: Unable to find the socket transport “http”

htmlPHPsockets

i want to send post variables with fsock,
but when i try this:

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

I get 'Unable to find the socket transport "http" ',
any ideas why?

Best Answer

fsockopen() opens a socket. Sockets do not know anything about Layer5+ protocols such as HTTP.

$fp = fsockopen('1.2.3.4', 80, $errno, $errstr, 30);

To request a certain path, send it in the request: GET /confirmation.html
To specify the domain, send it in the Host header: Host: 1.2.3.4


You might want to consider using the curl extension. There is usually no good reason to build HTTP requests manually in PHP.