php通过url获取信息,一个用php实现的获取URL信息的类

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

2021-01-22 20:01:18535

获取URL信息的类

使用这个类,你能获得URL的如下信息:

-Host

-Path

-Statuscode(eg.404,200,...)

-HTTPVersion

-Server

-ContentType

-Date

-ThewholeheaderstringoftheURL

复制代码 代码如下:

/**

*ClassforgettinginformationaboutURL's

*@authorSvenWagener

*@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

$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;

}

}

?>

复制代码 代码如下:

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:".$url->get_url()."
n";

echo"URLHost:".$url->get_url_host()."
n";

echo"URLPath:".$url->get_url_path()."
n
n";

echo"Statuscode:".$url->get_statuscode()."
n";

echo"Statuscodedescription:".$url->get_statuscode_desc()."
n";

echo"HTTPVersion:".$url->get_info_http_version()."
n";

echo"Server:".$url->get_info_server()."
n";

echo"ContentType:".$url->get_info_content_type()."
n";

echo"Date:".$url->get_info_date()."
n
n";

echo"WHOLEHEADERS:
n";

echo$url->get_header_stream();

?>

点击这里复制本文地址

以上内容由聚米学院网友整理呈现,如对侵犯您的权益,请联系邮箱:fzsbm@qq.com

留言评论

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值