获取URL信息的类
使用这个类,你能获得URL的如下信息:
-Host
-Path
-Statuscode(eg.404,200,...)
-HTTPVersion
-Server
-ContentType
-Date
-ThewholeheaderstringoftheURL
复制代码 代码如下:
<?
/**
*ClassforgettinginformationaboutURL's
*@authorSvenWagener<[email]sven.wagener@intertribe.de[/email]>
*@copyrightIntertribelimited
*@PHP中文社区收集整理[url]www.phpNet.cn[/url]
*@includeFunktion:_include_
*/
classurl{
var$url="";
var$url_host;
var$url_path;
var$file="";
var$code="";
var$code_desc="";
var$http_version="";//VariableforHTTPversion
var$header_stream;
var$header_array;
var$timeout="1";
/**
*Constructorofclassurl
*@paramstring$urlthecompleteurl
*@descConstructorofclassurl
*/
functionurl($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();
}
/**
*Returnsthewholeurl
*@returnstring$urlthewholeurl
*@descReturnsthewholeurl
*/
functionget_url(){
return$this->url;
}
/**
*Returnsthehostoftheurl
*@returnstring$url_hostthehostoftheurl
*@descReturnsthehostoftheurl
*/
functionget_url_host(){
return$this->url_host;
}
/**
*Returnsthepathoftheurl
*@returnstring$url_paththepathoftheurl
*@descReturnsthepathoftheurl
*/
functionget_url_path(){
return$this->url_path;
}
/**
*Returnsthestatuscodeoftheurl
*@returnstring$status_codethestatuscode
*@descReturnsthestatuscodeoftheurl
*/
functionget_statuscode(){
return$this->code;
}
/**
*Returnsthestatuscodedescriptionoftheurl
*@returnstring$status_code_descthestatuscodedescription
*@descReturnsthestatuscodedescriptionoftheurl
*/
functionget_statuscode_desc(){
return$this->code_desc;
}
/**
*Returnsthehttpversionoftheurlbythereturnedheadersoftheserver
*@returnstring$http_versionthehttpversion
*@descReturnsthehttpversionoftheurlbythereturnedheadersoftheserver
*/
functionget_info_http_version(){
return$this->http_version;
}
/**
*Returnstheservertypeoftheurl'shostbythereturnedheadersoftheserver
*@returnstringheader_array['Server']theservertype
*@descReturnstheservertypeoftheurl'shostbythereturnedheadersoftheserver
*/
functionget_info_server(){
return$this->header_array['Server'];
}
/**
*Returnsthedateoftheurl'shostbythereturnedheadersoftheserver
*@returnstring$header_array['Date']thedate
*@descReturnsthedateoftheurl'shostbythereturnedheadersoftheserver
*/
functionget_info_date(){
return$this->header_array['Date'];
}
/*
functionget_info_content_length(){
return$this->header_array['Content-Length'];
}
*/
/**
*Returnsthecontenttypebythereturnedheadersoftheserver
*@returnstringheader_array['Content-Type']thecontenttype
*@descReturnsthecontenttypebythereturnedheadersoftheserver
*/
functionget_info_content_type(){
return$this->header_array['Content-Type'];
}
/**
*Returnsthecontentoftheurlwithouttheheaders
*@returnstring$contentthecontent
*@descReturnsthecontentoftheurlwithouttheheaders
*/
functionget_content(){
//Getawebpageintoastring
$string=implode('',file($this->url));
return$string;
}
/**
*Returnsthewholeheaderofurlwithoutcontent
*@returnstring$headertheheader
*@descReturnsthewholeheaderofurlwithoutcontent
*/
functionget_header_stream(){
return$this->header_stream;
}
/**
*Returnsthewholeheadersoftheurlinanarray
*@returnarray$header_arraytheheadersinanarray
*@descReturnsthewholeheadersoftheurlinanarray
*/
functionget_headers(){
return$this->header_array;
}
/**
*Refreshestheheaderinformation
*@descRefreshestheheaderinformation
*/
functionrefresh_headerinfo(){
//Opensocketforconnectionviaport80toputheaders
$fp=fsockopen($this->url_host,80,$errno,$errstr,30);
if(!$fp){
//echo"$errstr($errno)";
if($errno==0){
$errstr="ServerNotFound";
}
$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;
//Gettingheaderstringandcreatingheaderarray
$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);
//GettingstatuscodeanddescriptionoftheURL
$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];
}
}
//Cleaningupfornandr
$this->code_desc=preg_replace("[n]","",$this->code_desc);
$this->code_desc=preg_replace("[r]","",$this->code_desc);
//GettingHttpVersion
$http_array=split("/",$splitted_stream[0]);
$this->http_version=$http_array[1];
}
}
/**
*Setsthetimeoutforgettingheaderdatafromserver
*@paramint$secondstimefortimeoutinseconds
*@descSetsthetimeoutforgettingheaderdatafromserver
*/
functionset_timeout($seconds){
$this->timeout=$seconds;
}
}
?>
复制代码 代码如下:
<?php
include("url.class.php");
$url=newurl("[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"URLHost:".$url->get_url_host()."<br>n";
echo"URLPath:".$url->get_url_path()."<br>n<br>n";
echo"Statuscode:".$url->get_statuscode()."<br>n";
echo"Statuscodedescription:".$url->get_statuscode_desc()."<br>n";
echo"HTTPVersion:".$url->get_info_http_version()."<br>n";
echo"Server:".$url->get_info_server()."<br>n";
echo"ContentType:".$url->get_info_content_type()."<br>n";
echo"Date:".$url->get_info_date()."<br>n<br>n";
echo"WHOLEHEADERS:<br>n";
echo$url->get_header_stream();
?>
【一个用php实现的获取URL信息的类】相关文章: