一个用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提交表单时判断 if($_POST[submit])与 if(isset($_POST[submit])) 的区别
Feb 08 PHP
php调整gif动画图片尺寸示例代码分享
Dec 05 PHP
一个非常完美的读写ini格式的PHP配置类分享
Feb 12 PHP
PHP中你应该知道的require()文件包含的正确用法
Jun 12 PHP
php调用淘宝开放API实现根据卖家昵称获取卖家店铺ID的方法
Jul 29 PHP
php文件类型MIME对照表(比较全)
Oct 07 PHP
php foreach如何跳出两层循环(详解)
Nov 05 PHP
PHP 搜索查询功能实现
Nov 29 PHP
Laravel5.1框架注册中间件的三种场景详解
Jul 09 PHP
php数组指针函数功能及用法示例
Feb 11 PHP
PHP实现简易用户登录系统
Jul 10 PHP
PHP连接SQL server数据库测试脚本运行实例
Aug 24 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实现链结人气统计
2006/10/09 PHP
PHP获取163、gmail、126等邮箱联系人地址【已测试2009.10.10】
2009/10/11 PHP
php读取csv文件后,uft8 bom导致在页面上显示出现问题的解决方法
2013/08/10 PHP
php CI框架插入一条或多条sql记录示例
2014/07/29 PHP
阿里云PHP SMS短信服务验证码发送方法
2017/07/11 PHP
PHP排序算法之冒泡排序(Bubble Sort)实现方法详解
2018/04/20 PHP
PHP获取远程http或ftp文件的md5值的方法
2019/04/15 PHP
基于jQuery实现的扇形定时器附源码下载
2015/10/20 Javascript
通过点击jqgrid表格弹出需要的表格数据
2015/12/02 Javascript
js实现C#的StringBuilder效果完整实例
2015/12/22 Javascript
JavaScript实现移动端滑动选择日期功能
2016/06/21 Javascript
js友好的时间返回函数
2016/08/24 Javascript
Node.js学习之查询字符串解析querystring详解
2017/09/28 Javascript
jQuery实现新闻播报滚动及淡入淡出效果示例
2018/03/23 jQuery
Vue 自适应高度表格的实现方法
2020/05/13 Javascript
JavaScript实现多文件下载方法解析
2020/08/07 Javascript
解决vue项目中遇到 Cannot find module ‘chalk‘ 报错的问题
2020/11/05 Javascript
[08:02]DOTA2牵红线 zhou神抱得美人归
2014/03/22 DOTA
Python计算一个文件里字数的方法
2015/06/15 Python
用python简单实现mysql数据同步到ElasticSearch的教程
2018/05/30 Python
利用python对Excel中的特定数据提取并写入新表的方法
2018/06/14 Python
详解Python给照片换底色(蓝底换红底)
2019/03/22 Python
用python建立两个Y轴的XY曲线图方法
2019/07/08 Python
Django框架HttpResponse对象用法实例分析
2019/11/01 Python
python代码打印100-999之间的回文数示例
2019/11/24 Python
TensorFlow实现自定义Op方式
2020/02/04 Python
python等差数列求和公式前 100 项的和实例
2020/02/25 Python
jupyter notebook 重装教程
2020/04/16 Python
python实现数据结构中双向循环链表操作的示例
2020/10/09 Python
详解html5 shiv.js和respond.min.js
2018/01/24 HTML / CSS
家乐福巴西网上超市:Carrefour巴西
2016/10/31 全球购物
肯尼迪就职演说稿
2013/12/31 职场文书
教师现实表现材料
2014/02/14 职场文书
奥巴马获胜演讲稿
2014/05/15 职场文书
公司租车协议书
2015/01/29 职场文书
Python selenium的这三种等待方式一定要会!
2021/06/10 Python