牛骨文教育服务平台(让学习变的简单)
博文笔记

详解PHP fsockopen的使用方法(获取页面的头信息)

创建时间:2011-05-23 投稿人: 浏览次数:2229

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. }  
声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。