A class that gets URL information implemented in PHP

Source: Internet
Author: User

class to get URL information

Using this class, you can get the following information for the URL:

-Host
-Path
-Statuscode (eg. 404,200, ...)
-HTTP Version
-Server
-Content Type
-Date
-the whole header string of the URL
  Copy CodeThe code is as follows:
<?
/**
* Class for getting information about URL ' s
* @author Sven Wagener <[email][email protected][/email]>
* @copyright Intertribe Limited
* Collection and collation of @PHP Chinese community [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 and put headers
$fp = Fsockopen ($this->url_host, $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) {
$line 1= $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 ("", $line 1);

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 headers data from server
* @param int $seconds time for timeout in seconds
* @desc sets the timeout for getting headers data from server
*/
function Set_timeout ($seconds) {
$this->timeout= $seconds;
}
}
?>
  Copy CodeThe code is as follows:
<?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 ();
?>

A class that gets URL information implemented in PHP

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.