一个用php实现的获取URL信息的类


Posted in PHP onJanuary 02, 2007

获取URL信息的类

使用这个类,你能获得URL的如下信息:

- Host 
- Path 
- Statuscode (eg. 404,200, ...) 
- HTTP Version 
- Server 
- Content Type 
- Date 
- The whole header string of the URL

<? 
/** 
* Class for getting information about URL's 
* @author    Sven Wagener <[email]sven.wagener@intertribe.de[/email]> 
* @copyright Intertribe limited 
* @PHP中文社区收集整理 [url]www.phpNet.cn[/url] 
* @include          Funktion:_include_ 
*/ 
class url{   var $url=""; 
  var $url_host; 
  var $url_path; 
  var $file=""; 
  var $code=""; 
  var $code_desc=""; 
  var $http_version=""; // Variable for HTTP version 
  var $header_stream; 
  var $header_array; 
  var $timeout="1"; 
  /** 
  * Constructor of class url 
  * @param string        $url the complete url 
  * @desc Constructor of class url 
  */ 
  function url($url){ 
    $this->url=$url; 
    $url_array=parse_url($this->url); 
    $this->url_host=$url_array['host']; 
    $this->url_path=$url_array['path']; 
    if($this->url_path==""){ 
            $this->url_path="/"; 
    } 
    $this->refresh_headerinfo(); 
  } 
  /** 
  * Returns the whole url 
  * @return string $url the whole url 
  * @desc Returns the whole url 
  */ 
  function get_url(){ 
          return $this->url; 
  } 
  /** 
  * Returns the host of the url 
  * @return string $url_host the host of the url 
  * @desc Returns the host of the url 
  */ 
  function get_url_host(){ 
    return $this->url_host; 
  } 
  /** 
  * Returns the path of the url 
  * @return string $url_path the path of the url 
  * @desc Returns the path of the url 
  */ 
  function get_url_path(){ 
    return $this->url_path; 
  } 
  /** 
  * Returns the status code of the url 
  * @return string $status_code the status code 
  * @desc Returns the status code of the url 
  */ 
  function get_statuscode(){ 
    return $this->code; 
  } 
  /** 
  * Returns the status code description of the url 
  * @return string $status_code_desc the status code description 
  * @desc Returns the status code description of the url 
  */ 
  function get_statuscode_desc(){ 
    return $this->code_desc; 
  } 
  /** 
  * Returns the http version of the url by the returned headers of the server 
  * @return string $http_version the http version 
  * @desc Returns the http version of the url by the returned headers of the server 
  */ 
  function get_info_http_version(){ 
    return $this->http_version; 
  } 
  /** 
  * Returns the server type of the url's host by the returned headers of the server 
  * @return string header_array['Server'] the server type 
  * @desc Returns the server type of the url's host by the returned headers of the server 
  */ 
  function get_info_server(){ 
    return $this->header_array['Server']; 
  } 
  /** 
  * Returns the date of the url's host by the returned headers of the server 
  * @return string $header_array['Date'] the date 
  * @desc Returns the date of the url's host by the returned headers of the server 
  */ 
  function get_info_date(){ 
    return $this->header_array['Date']; 
  } 
  /* 
  function get_info_content_length(){ 
    return $this->header_array['Content-Length']; 
  } 
  */ 
  /** 
  * Returns the content type by the returned headers of the server 
  * @return string header_array['Content-Type'] the content type 
  * @desc Returns the content type by the returned headers of the server 
  */ 
  function get_info_content_type(){ 
    return $this->header_array['Content-Type']; 
  } 
  /** 
  * Returns the content of the url without the headers 
  * @return string $content the content 
  * @desc Returns the content of the url without the headers 
  */ 
  function get_content(){ 
    // Get a web page into a string 
    $string = implode ('', file ($this->url)); 
    return $string; 
  } 
  /** 
  * Returns the whole header of url without content 
  * @return string $header the header 
  * @desc Returns the whole header of url without content 
  */ 
  function get_header_stream(){ 
    return $this->header_stream; 
  } 
  /** 
  * Returns the whole headers of the url in an array 
  * @return array $header_array the headers in an array 
  * @desc Returns the whole headers of the url in an array 
  */ 
  function get_headers(){ 
    return $this->header_array; 
  } 
  /** 
  * Refreshes the header information 
  * @desc Refreshes the header information 
  */ 
  function refresh_headerinfo(){ 
    // Open socket for connection via port 80 to put headers 
    $fp = fsockopen ($this->url_host, 80, $errno, $errstr, 30); 
    if (!$fp) { 
      // echo "$errstr ($errno)"; 
      if($errno==0){ 
              $errstr="Server Not Found"; 
      } 
      $this->code=$errno; 
      $this->code_desc=$errstr; 
    } else { 
      $put_string="GET ".$this->url_path." HTTP/1.0rnHost: ".$this->url_host."rnrn"; 
      fputs ($fp, $put_string); 
      @socket_set_timeout($fp,$this->timeout); 
      $stream=""; 
      $this->header_array=""; 
      $header_end=false; 
      // Getting header string and creating header array 
      $i=0; 
      while (!feof($fp) && !$header_end) { 
        $line=fgets($fp,128); 
        if(strlen($line)==2){ 
          $header_end=true; 
        }else{ 
          if($i==0){ 
            $line1=$line; 
          } 
          $stream.=$line; 
          $splitted_line=split(":",$line); 
          $this->header_array[$splitted_line[0]]=$splitted_line[1]; 
          $i++; 
        } 
      } 
      fclose ($fp); 
      $this->header_stream=$stream; 
      $splitted_stream=split(" ",$line1); 
      // Getting status code and description of the URL 
      $this->code=$splitted_stream[1]; 
      $this->code_desc=$splitted_stream[2]; 
      if(count($splitted_stream)>3){ 
        for($i=3;$i<count($splitted_stream);$i++){ 
          $this->code_desc.=" ".$splitted_stream[$i]; 
        } 
      } 
      // Cleaning up for n and r 
      $this->code_desc=preg_replace("[\n]","",$this->code_desc); 
      $this->code_desc=preg_replace("[\r]","",$this->code_desc); 
      // Getting Http Version 
      $http_array=split("/",$splitted_stream[0]); 
      $this->http_version=$http_array[1]; 
      } 
  } 
  /** 
  * Sets the timeout for getting header data from server 
  * @param int $seconds time for timeout in seconds 
  * @desc Sets the timeout for getting header data from server 
  */ 
  function set_timeout($seconds){ 
    $this->timeout=$seconds; 
  } 
} 
?>

<?php  
include("url.class.php"); 
$url=new url("[url]http://www.phpNet.cn/[/url]"); echo $url->get_header_stream(); 
$headers=$url->get_headers(); 
echo $headers['Server']; 
echo $url->get_content(); 
echo "URL: <b>".$url->get_url()."</b><br>n"; 
echo "URL Host: ".$url->get_url_host()."<br>n"; 
echo "URL Path: ".$url->get_url_path()."<br>n<br>n"; 
echo "Statuscode: ".$url->get_statuscode()."<br>n"; 
echo "Statuscode description: ".$url->get_statuscode_desc()."<br>n"; 
echo "HTTP Version: ".$url->get_info_http_version()."<br>n"; 
echo "Server: ".$url->get_info_server()."<br>n"; 
echo "Content Type: ".$url->get_info_content_type()."<br>n"; 
echo "Date: ".$url->get_info_date()."<br>n<br>n"; 
echo "WHOLE HEADERS:<br>n"; 
echo $url->get_header_stream(); 
?>
PHP 相关文章推荐
PHP截取中文字符串的问题
Jul 12 PHP
强烈推荐:php.ini中文版(2)
Oct 09 PHP
php 变量定义方法
Jun 14 PHP
php cout&amp;lt;&amp;lt;的一点看法
Jan 24 PHP
php 数组动态添加实现代码(最土团购系统的价格排序)
Dec 30 PHP
深入PHP nl2br()格式化输出的详解
Jun 05 PHP
php支持中文字符串分割的函数
May 28 PHP
PHP判断来访是搜索引擎蜘蛛还是普通用户的代码小结
Sep 14 PHP
详解php设置session(过期、失效、有效期)
Nov 12 PHP
PHP操作mysql数据库分表的方法
Jun 09 PHP
PHP数组实例详解
Jun 26 PHP
php事件驱动化设计详解
Nov 10 PHP
PHP 和 MySQL 开发的 8 个技巧
Jan 02 #PHP
Smarty结合Ajax实现无刷新留言本实例
Jan 02 #PHP
Ajax PHP分页演示
Jan 02 #PHP
windows下PHP APACHE MYSQ完整配置
Jan 02 #PHP
PHP Ajax实现页面无刷新发表评论
Jan 02 #PHP
PHP+AJAX实现无刷新注册(带用户名实时检测)
Jan 02 #PHP
新手学PHP之数据库操作详解及乱码解决!
Jan 02 #PHP
You might like
投票管理程序
2006/10/09 PHP
PHP函数学习之PHP函数点评
2012/07/05 PHP
解析CI即CodeIgniter框架在Nginx下的重写规则
2013/06/03 PHP
Php-Redis安装测试笔记
2015/03/05 PHP
php二维码生成
2015/10/19 PHP
通过chrome浏览器控制台(Console)进行PHP Debug的方法
2016/10/19 PHP
编写兼容IE和FireFox的脚本
2009/05/18 Javascript
字符串的replace方法应用浅析
2011/12/06 Javascript
jquery ajax修改全局变量示例代码
2013/11/08 Javascript
jquery的each方法使用示例分享
2014/03/25 Javascript
深入了解Node.js中的一些特性
2014/09/25 Javascript
使用angular写一个hello world
2015/01/23 Javascript
jQuery 获取页面li数组并删除不在数组中的key
2016/08/02 Javascript
又一款js时钟!transform实现时钟效果
2016/08/15 Javascript
JS基于正则实现数字千分位用逗号分隔的方法
2017/06/16 Javascript
关于JavaScript的单双引号嵌套问题
2017/08/20 Javascript
Mpvue中使用Vant Weapp组件库的方法步骤
2019/05/16 Javascript
Python中使用select模块实现非阻塞的IO
2015/02/03 Python
Python模拟登录验证码(代码简单)
2016/02/06 Python
Python enumerate索引迭代代码解析
2018/01/19 Python
python的re正则表达式实例代码
2018/01/24 Python
Django 视图层(view)的使用
2018/11/09 Python
解决python执行不输出系统命令弹框的问题
2019/06/24 Python
python异步实现定时任务和周期任务的方法
2019/06/29 Python
django inspectdb 操作已有数据库数据的使用步骤
2021/02/07 Python
python 获取域名到期时间的方法步骤
2021/02/10 Python
Python对excel的基本操作方法
2021/02/18 Python
matplotlib绘制正余弦曲线图的实现
2021/02/22 Python
美国最大最全的亚洲购物网站:美国亚米网(Yamibuy)
2020/05/05 全球购物
广告设计专业自荐信范文
2013/11/14 职场文书
矫正人员思想汇报
2014/01/08 职场文书
大学迎新晚会主持词
2014/03/24 职场文书
家长会标语
2014/06/24 职场文书
玩手机检讨书1000字
2014/10/20 职场文书
先进个人总结范文
2015/02/15 职场文书
hive数据仓库新增字段方法
2022/06/25 数据库