利用fsockopen可实现异步成功访问

时间:2023-03-09 01:44:48
利用fsockopen可实现异步成功访问

$fp = fsockopen("www.jb51.net", 80, $errno, $errstr, 30);
if (!$fp) {
  echo "$errstr ($errno) ";
} else {
  $out = "get / http/1.1 ";
  $out .= "host: www.jb51.net";
  $out .= "connection: close ";
  fwrite($fp, $out);
  while (!feof($fp)) {
    echo fgets($fp, 128);
  }
    fclose($fp);
}

说明:
resource fsockopen ( string $hostname [, int $port = -1 [, int &$errno [, string &$errstr [, float $timeout = ini_get("default_socket_timeout") ]]]] )

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

设置连接的时限,单位为秒。