fsock:无法找到套接字传输“http” [英] fsock: Unable to find the socket transport "http"

查看:180
本文介绍了fsock:无法找到套接字传输“http”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  $ post_arr = array 

我想用fsock,
发送post变量,但是当我尝试这个时: (a=>b);
$ addr ='http://1.2.3.4/confirmation.html';

$ fp = fsockopen($ addr,80,$ errno,$ errstr,30);
if(!$ fp){
echo$ errstr($ errno)< br /> gt; \\\
;
} else {

$ req ='';
foreach($ post_arr as $ key => $ value){
$ value = urlencode(stripslashes($ value));
$ req。=& 。 $键。 =。 $值;
}


$ header =POST / cgi-bin / webscr HTTP / 1.0 \r\\\
;
$ header。=Content-Type:application / x-www-form-urlencoded \r\\\
;
$ header。=Content-Length:。 strlen($ req)。 \r\\\
\r\\\
;
fwrite($ fp,$ header);
while(!feof($ fp)){
echo fgets($ fp,128);
}
fclose($ fp);
}

我得到'无法找到套接字传输'http'',
任何想法为什么?

解决方案

fsockopen()打开一个套接字。套接字对Layer5 +协议(例如HTTP)一无所知。

errno,$ errstr,30);

要请求某个路径,请发送请求: GET /确认.html

要指定域名,请将其发送到主机标头中:主机:1.2 .3.4






您可能要考虑使用 curl 扩展名。在PHP中手动构建HTTP请求通常没有什么好的理由。


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?

解决方案

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.

这篇关于fsock:无法找到套接字传输“http”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆