PHP中fsockopen如何使用

本篇文章为大家展示了PHP中fsockopen如何使用,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

1.PHP fsockopen函数说明:

Open Internet or Unix domain socket connection(打开套接字链接)

Initiates a socket connection to the resource specified by target .

fsockopen() returns a file pointer which may be used together with the other file functions (such as fgets() , fgetss() , fwrite() , fclose() , and feof() ).就是返回一个文件句柄

开启PHP fsockopen这个函数

PHP fsockopen需要 PHP.ini 中 allow_url_fopen 选项开启。

  1. $fp = fsockopen("www.example.com",
     80, $errno, $errstr, 30);   

  2. if (!$fp) {   

  3. echo "$errstr ($errno)<br />\n";   

  4. } else {   

  5. $out = "GET / HTTP/1.1\r\n";   

  6. $out .= "Host: www.example.com\r\n";   

  7. $out .= "Connection: Close\r\n\r\n";   

  8.  

  9. fwrite($fp, $out);   

  10. while (!feof($fp)) {   

  11. echo fgets($fp, 128);   

  12. }   

  13. fclose($fp);   

  14. }  

上述内容就是PHP中fsockopen如何使用,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注行业资讯频道。