来自AI助手的总结
                fsockopen用于建立网络或Unix套接字连接,可指定参数、错误处理和超时,并返回文件句柄。
            fsockopen — 打开一个网络连接或者一个Unix套接字连接
参数
hostname
如果安装了OpenSSL,那么你也许应该在你的主机名地址前面添加访问协议ssl://或者是tls://,从而可以使用基于TCP/IP协议的SSL或者TLS的客户端连接到远程主机。
port
端口号。如果对该参数传一个-1,则表示不使用端口,例如unix://。
errno
如果传入了该参数,holds the system level error number that occurred in the system-level connect() call。
如果errno的返回值为0,而且这个函数的返回值为FALSE,那么这表明该错误发生在套接字连接(connect())调用之前,导致连接失败的原因最大的可能是初始化套接字的时候发生了错误。
errstr
错误信息将以字符串的信息返回。
timeout
设置连接的时限,单位为秒。
注意:如果你要对建立在套接字基础上的读写操作设置操作时间设置连接时限,请使用stream_set_timeout(),fsockopen()的连接时限(timeout)的参数仅仅在套接字连接的时候生效。
返回值
fsockopen()将返回一个文件句柄,之后可以被其他文件类函数调用(例如:fgets(),fgetss(),fwrite(),fclose()还有feof())。如果调用失败,将返回FALSE。
示例一
n";
} else {
    $out = "GET / HTTP/1.1rn";
    $out .= "Host: www.example.comrn";
    $out .= "Connection: Closernrn";
    fwrite($fp, $out);
    while (!feof($fp)) {
        echo fgets($fp, 128);
    }
    fclose($fp);
}
?>
示例二
主要用于推送微信消息模板
//异步发送
openid = '';
access_token = '';
$redirect_uri = cmf_url('weixin/Index/login');
$data = array(
    "touser"      => $openid,
    "template_id" => "jSx2-LE5ceOzKXof24IRBOoZ6iOmeAlol-r5fBvK_QA",
    "url"         => $redirect_uri,
    "data"        => array(
        "first"    => array("value" => $company_data['company'].'-'.$key),
        "keyword1" => array("value" => $fxmonth),
        "keyword2" => array("value" => $payroll['title']),
    ),
);
$params         = json_encode($data, JSON_UNESCAPED_UNICODE);
$fp = fsockopen('api.weixin.qq.com', 80, $error, $errstr, 1);
$http = "POST /cgi-bin/message/template/send?access_token={$access_token} HTTP/1.1rnHost: api.weixin.qq.comrnContent-type: application/x-www-form-urlencodedrnContent-Length: " . strlen($params) . "rnConnection:closernrn$paramsrnrn";
fwrite($fp, $http);
fclose($fp);
示例三
下面这个例子展示了怎么样在自己的机器上通过UDP套接字连接(端口号13)来检索日期和时间。
n";
} else {
    fwrite($fp, "n");
    echo fread($fp, 26);
    fclose($fp);
}
?>
© 版权声明
THE END
    








暂无评论内容