<p>打开网络的 Socket 链接。</p><p>语法: int fsockopen(string hostname, int port, int [errno], string [errstr], int [timeout]);</p><p>返回值: 整数</p><p>函数种类: 网络系统</p><p>内容说明</p><p>目前这个函数提供二个 Socket 资料流界面,分别为 Internet 用的 AF_INET 及 Unix 用的 AF_UNIX。当在 Internet 情形下使用时,参数 hostname 及 port 分别代表网址及埠号。在 UNIX 情形可做 IPC,hostname 参数表示到 socket 的路径,port 配置为 0。可省略的 timeout 选项表示多久没有连上就中断。在使用本函数之后会返回文件指针,供文件函数使用,包括 fgets()、fgetss()、fputs()、fclose() 与 feof()。参数 errno 及 errstr 也是可省略的,主要当做错误处理使用。使用本函数,会使用搁置模式 (blocking mode) 处理,可用 set_socket_blocking() 转换成无搁置模式。</p><p>使用范例</p><p>本例用来模拟成 HTTP 连接。</p><pre class="brush:php;toolbar:false">&lt;?php $fp=fsockopen(&quot;php.wilson.gs&quot;,80,&amp;$errno,&amp;$errstr,10); if(!$fp){ echo&quot;$errstr($errno)&lt;br&gt;\n&quot;; }else{ fputs($fp,&quot;GET/HTTP/1.0\nHost:php.wilson.gs\n\n&quot;); while(!feof($fp)){ echofgets($fp,128); } fclose($fp); } ?&gt;</pre>
T:0.007077s,M:245.57 KB
返回顶部 留言