时间:2021-05-25
如果支持打开远程内容的选项的话,实际上php用fopen或file_get_contents都能获得一个网页的内容,但是默认的函数有个不足的地方就是无法获取HTTP头,这在一些特殊的应用中很不方便,如,有一个链接:
http://patible;MSIE6.0;WindowsNT5.2)";}
if(!isset($this->m_puthead["Refer"])){$this->m_puthead["Refer"]="http://".$this->m_puthead["Host"];}
foreach($this->m_putheadas$k=>$v){
$k=trim($k);
$v=trim($v);
if($k!=""&&$v!=""){
fputs($this->m_fp,"$k:$v\r\n");
}
}
//发送固定的结束请求头
//HTTP1.1协议必须指定文档结束后关闭链接,否则读取文档时无法使用feof判断结束
if($httpv=="HTTP/1.1")fputs($this->m_fp,"Connection:Close\r\n\r\n");
elsefputs($this->m_fp,"\r\n");
//获取应答头状态信息
$httpstas=explode("",fgets($this->m_fp,256));
$this->m_httphead["http-edition"]=trim($httpstas[0]);
$this->m_httphead["http-state"]=trim($httpstas[1]);
$this->m_httphead["http-describe"]="";
for($i=2;$i<count($httpstas);$i++){
$this->m_httphead["http-describe"].="".trim($httpstas[$i]);
}
//获取详细应答头
while(!feof($this->m_fp)){
$line=trim(fgets($this->m_fp,256));
if($line=="")break;
$hkey="";
$hvalue="";
$v=0;
for($i=0;$i<strlen($line);$i++){
if($v==1)$hvalue.=$line[$i];
if($line[$i]==":")$v=1;
if($v==0)$hkey.=$line[$i];
}
$hkey=trim($hkey);
if($hkey!="")$this->m_httphead[strtolower($hkey)]=trim($hvalue);
}
//判断是否是3xx开头的应答
if(ereg("^3",$this->m_httphead["http-state"]))
{
if($this->JumpCount>3)return;
if(isset($this->m_httphead["location"])){
$newurl=$this->m_httphead["location"];
if(eregi("^http",$newurl)){
$this->JumpOpenUrl($newurl);
}
else{
$newurl=$this->FillUrl($newurl);
$this->JumpOpenUrl($newurl);
}
}
else
{$this->m_error="无法识别的转移应答!";}
}//
}
//
//获得一个Http头的值
//
functionGetHead($headname)
{
$headname=strtolower($headname);
if(isset($this->m_httphead[$headname]))
return$this->m_httphead[$headname];
else
return"";
}
//
//设置Http头的值
//
functionSetHead($skey,$svalue)
{
$this->m_puthead[$skey]=$svalue;
}
//
//打开连接
//
functionPrivateOpenHost()
{
if($this->m_host=="")returnfalse;
$this->m_fp=@fsockopen($this->m_host,$this->m_port,&$errno,&$errstr,10);
if(!$this->m_fp){
$this->m_error=$errstr;
returnfalse;
}
else{
returntrue;
}
}
//
//关闭连接
//
functionClose(){
@fclose($this->m_fp);
}
//
//补全相对网址
//
functionFillUrl($surl)
{
$i=0;
$dstr="";
$pstr="";
$okurl="";
$pathStep=0;
$surl=trim($surl);
if($surl=="")return"";
$pos=strpos($surl,"#");
if($pos>0)$surl=substr($surl,0,$pos);
if($surl[0]=="/"){
$okurl="http://".$this->HomeUrl."/".$surl;
}
elseif($surl[0]==".")
{
if(strlen($surl)<=2)return"";
elseif($surl[0]=="/")
{
$okurl="http://".$this->BaseUrlPath."/".substr($surl,2,strlen($surl)-2);
}
else{
$urls=explode("/",$surl);
foreach($urlsas$u){
if($u=="..")$pathStep++;
elseif($i<count($urls)-1)$dstr.=$urls[$i]."/";
else$dstr.=$urls[$i];
$i++;
}
$urls=explode("/",$this->BaseUrlPath);
if(count($urls)<=$pathStep)
return"";
else{
$pstr="http://";
for($i=0;$i<count($urls)-$pathStep;$i++)
{$pstr.=$urls[$i]."/";}
$okurl=$pstr.$dstr;
}
}
}
else
{
if(strlen($surl)<7)
$okurl="http://".$this->BaseUrlPath."/".$surl;
elseif(strtolower(substr($surl,0,7))=="http://")
$okurl=$surl;
else
$okurl="http://".$this->BaseUrlPath."/".$surl;
}
$okurl=eregi_replace("^(http://)","",$okurl);
$okurl=eregi_replace("/{1,}","/",$okurl);
return"http://".$okurl;
}
}
?>
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
用PHP写一个验证码类,并进行封装。类名:validationcode.class.php代码如下:复制代码代码如下:
今天试着写一个PHP与C语言通过socket通讯的程序,看过PHP手册,发现有好几种方式可以建立socket客户端.1、通过fsockopen()建立socke
这几天在写一个PHP防盗链外部资源下载处理函数,昨天晚上刚完成编写,中间遇到了些问题,这里就不详述了;以下是自写的简单的PHP防盗链处理类(重新整理编写成类文件
今天研究下pear的cache_lite。从http://pear.php.net上下载了最新的cache_lite。cache_lite是pear库类中一个轻
它是一个开源软件,是完全采用PHP语言编写的、通过HTTP收发SOAP消息的一系列PHP类,由NuSphereCorporation(http://dietri