一个用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 和 MySQL 基础教程(三)
Oct 09 PHP
phpMyAdmin 安装教程全攻略
Mar 19 PHP
php下关于中英数字混排的字符串分割问题
Apr 06 PHP
Php Ctemplate引擎开发相关内容
Mar 03 PHP
php网站地图生成类示例
Jan 13 PHP
初识php MVC
Sep 10 PHP
php传值赋值和传地址赋值用法实例分析
Jun 20 PHP
PHP实现批量上传单个文件
Dec 29 PHP
PHP中如何防止外部恶意提交调用ajax接口
Apr 11 PHP
PHP中子类重载父类的方法【parent::方法名】
May 06 PHP
workerman结合laravel开发在线聊天应用的示例代码
Oct 30 PHP
PHP htmlspecialchars()函数用法与实例讲解
Mar 08 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
php 文件上传代码(限制jpg文件)
2010/01/05 PHP
关于mysql字符集设置了character_set_client=binary 在gbk情况下会出现表描述是乱码的情况
2013/01/06 PHP
PHP命名空间(Namespace)简明教程
2014/06/11 PHP
Yii中CGridView关联表搜索排序方法实例详解
2014/12/03 PHP
PHP 魔术变量和魔术函数详解
2015/02/25 PHP
javascript css在IE和Firefox中区别分析
2009/02/18 Javascript
基于JQuery的列表拖动排序实现代码
2013/10/01 Javascript
javascript中eval函数用法分析
2015/04/25 Javascript
JS+CSS实现带有碰撞缓冲效果的竖向导航条代码
2015/09/15 Javascript
深入理解jQuery中的事件冒泡
2016/05/24 Javascript
轻松掌握JavaScript中的Math object数学对象
2016/05/26 Javascript
jquery.qtip提示信息插件用法简单实例
2016/06/17 Javascript
微信小程序使用第三方库Immutable.js实例详解
2016/09/27 Javascript
JavaScript中全选、全不选、反选、无刷新删除、批量删除、即点即改入库(在yii框架中操作)的代码分享
2016/11/01 Javascript
深入理解JavaScript 参数按值传递
2017/05/24 Javascript
javascript 缓冲运动框架的实现
2017/09/29 Javascript
对vue中v-on绑定自定事件的实例讲解
2018/09/06 Javascript
实现Vue的markdown文档可以在线运行的方法示例
2018/12/11 Javascript
uni-app自定义导航栏按钮|uniapp仿微信顶部导航条功能
2019/11/12 Javascript
vue实现element表格里表头信息提示功能(推荐)
2019/11/20 Javascript
Jquery属性的获取/设置及样式添加/删除操作技巧分析
2019/12/23 jQuery
JavaScript单线程和任务队列原理解析
2020/02/04 Javascript
vue 遮罩层阻止默认滚动事件操作
2020/07/28 Javascript
用Python进行基础的函数式编程的教程
2015/03/31 Python
Python Web程序部署到Ubuntu服务器上的方法
2018/02/22 Python
Python3爬虫学习之将爬取的信息保存到本地的方法详解
2018/12/12 Python
python 动态迁移solr数据过程解析
2019/09/04 Python
python 函数的缺省参数使用注意事项分析
2019/09/17 Python
如何在pycharm中安装第三方包
2020/10/27 Python
巧用CSS3的calc()宽度计算做响应模式布局的方法
2018/03/22 HTML / CSS
HTML5如何实现元素拖拽
2016/03/11 HTML / CSS
全球最大的户外用品零售商之一:The House
2018/06/12 全球购物
2014年社会实践活动总结范文
2014/04/29 职场文书
Django给表单添加honeypot验证增加安全性
2021/05/06 Python
python如何正确使用yield
2021/05/21 Python
Java 在线考试云平台的实现
2021/11/23 Java/Android