Home php教程 php手册 一个用php实现的获取URL信息的类

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

Jun 13, 2016 pm 12:34 PM
php url use information accomplish use of kind Obtain get this

获取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 
* @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          $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;
  }
}
?>


复制代码 代码如下:


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: ".$url->get_url()."
n";
echo "URL Host: ".$url->get_url_host()."
n";
echo "URL Path: ".$url->get_url_path()."
n
n";

echo "Statuscode: ".$url->get_statuscode()."
n";
echo "Statuscode description: ".$url->get_statuscode_desc()."
n";
echo "HTTP Version: ".$url->get_info_http_version()."
n";
echo "Server: ".$url->get_info_server()."
n";
echo "Content Type: ".$url->get_info_content_type()."
n";
echo "Date: ".$url->get_info_date()."
n
n";

echo "WHOLE HEADERS:
n";
echo $url->get_header_stream();
?>

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

See all articles