关于php操作http header

1、用 php 获取 http header 信息

<?php
$headers = getallheaders();
print_r($headers);
?>

2、用 php 生成 http header 信息

3、php 直接用 socket 来产生和读取相应的 http header 信息,参考例程如下:

引用自(http://www.bigbold.com/snippets/posts/show/1207

function remote_file_size ($url){
$head = "";
$url_p = parse_url($url);
$host = $url_p["host"];
if(!preg_match("/[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*/",$host)){
// a domain name was given, not an IP
$ip=gethostbyname($host);
if(!preg_match("/[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*/",$ip)){
//domain could not be resolved
return -1;
}
}
$port = intval($url_p["port"]);
if(!$port) $port=80;
$path = $url_p["path"];
//echo "Getting " . $host . ":" . $port . $path . " ...";

$fp = fsockopen($host, $port, $errno, $errstr, 20);
if(!$fp) {
return false;
} else {
fputs($fp, "HEAD " . $url . " HTTP/1.1\r\n");
fputs($fp, "HOST: " . $host . "\r\n");
fputs($fp, "User-Agent: http://www.example.com/my_application\r\n");
fputs($fp, "Connection: close\r\n\r\n");
$headers = "";
while (!feof($fp)) {
$headers .= fgets ($fp, 128);
}
}
fclose ($fp);
//echo $errno .": " . $errstr . "<br />";
$return = -2;
$arr_headers = explode("\n", $headers);
// echo "HTTP headers for <a href='" . $url . "'>..." . substr($url,strlen($url)-20). "</a>:";
// echo "<div class='http_headers'>";
foreach($arr_headers as $header) {
// if (trim($header)) echo trim($header) . "<br />";
$s1 = "HTTP/1.1";
$s2 = "Content-Length: ";
$s3 = "Location: ";
if(substr(strtolower ($header), 0, strlen($s1)) == strtolower($s1)) $status = substr($header, strlen($s1));
if(substr(strtolower ($header), 0, strlen($s2)) == strtolower($s2)) $size = substr($header, strlen($s2));
if(substr(strtolower ($header), 0, strlen($s3)) == strtolower($s3)) $newurl = substr($header, strlen($s3));
}
// echo "</div>";
if(intval($size) > 0) {
$return=intval($size);
} else {
$return=$status;
}
// echo intval($status) .": [" . $newurl . "]<br />";
if (intval($status)==302 && strlen($newurl) > 0) {
// 302 redirect: get HTTP HEAD of new URL
$return=remote_file_size($newurl);
}
return $return;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值