首页 > 编程知识 正文

关于php的头信息的简单介绍

时间:2023-12-29 13:16:44 阅读:330066 作者:YHVC

本文目录一览:

php 获取网页头部信息和网页和网页源代码查看

?php

/**

* http下载类库

*/

class Httplib{

// 目标网站无法打开时返回的错误代码

var $_ERROR_CONNECT_FAILURE = 600;

// 自定义 UserAgent 字符串

var $_SEND_USER_AGENT = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; LazyCMS.net::DownLoader)';

var $_url,$_method,$_timeout;

var $_scheme,$_host,$_port,$_path,$_query,$_referer;

var $_header;

var $_response;

/**

* 兼容PHP5模式

*

* @param 同下面的参数

*/

function __construct($url=null,$method='GET',$timeout=60){

@set_time_limit(0);

if (!empty($url)) {

$this-connect($url,$method,$timeout);

}

return $this;

}

/**

* 初始化对象

*

* @param string $url

* @param string $method

* @param int $timeout

* @return object

*/

function Httplib($url=null,$method='GET',$timeout=60){

return $this-__construct($url,$method,$timeout);

}

/**

* 改变连接url

*

* @param string $url

* @param string $method

* @param int $timeout

* @return object

*/

function connect($url=null,$method='GET',$timeout=60){

$this-_header = null;

$this-_response = null;

$this-_url = $url;

$this-_method = strtoupper(empty($method) ? 'GET' : $method);

$this-_timeout = empty($timeout) ? 30 : $timeout;

if (!empty($url)) {

$this-_parseURL($url);

}

return $this;

}

/**

* 发送请求

*

* @param array $params

* @return bool

*/

function send($params=array()) {

$header = null; $response = null; $QueryStr = null;

if (!empty($params)) { $this-_method = 'POST'; }

if (function_exists('fsockopen')) {

$fp = @fsockopen($this-_host,$this-_port,$errno,$errstr,$this-_timeout);

if (!$fp) { return false; }

$_port = ((int)$this-_port!==80) ? ':'.$this-_port : null;

$SendStr = "{$this-_method} {$this-_path}{$this-_query} HTTP/1.0rn";

$SendStr.= "Host:{$this-_host}{$_port}rn";

$SendStr.= "Accept: */*rn";

$SendStr.= "Referer:{$this-_referer}rn";

$SendStr.= "User-Agent: ".$this-_SEND_USER_AGENT."rn";

$SendStr.= "Pragma: no-cachern";

$SendStr.= "Cache-Control: no-cachern";

//如果是POST方法,分析参数

if ($this-_method=='POST') {

//判断参数是否是数组,循环出查询字符串

if (is_array($params)) {

$QueryStr = http_build_query($params);

} else {

$QueryStr = $params;

}

$length = strlen($QueryStr);

$SendStr.= "Content-Type: application/x-www-form-urlencodedrn";

$SendStr.= "Content-Length: {$length}rn";

}

$SendStr.= "Connection: Closernrn";

if(strlen($QueryStr) 0){

$SendStr.= $QueryStr."rn";

}

fputs($fp,$SendStr);

// 读取 header

do{ $header.= fread($fp,1); } while (!preg_match("/rnrn$/",$header));

// 遇到跳转,执行跟踪跳转

if ($this-_redirect($header)) { return true; }

// 读取内容

while(!feof($fp)) {

$response.= fread($fp,4096);

}

fclose($fp);

} elseif (function_exists('curl_exec')) {

$ch = curl_init($this-_url);

curl_setopt_array($ch,array(

CURLOPT_TIMEOUT = $this-_timeout,

CURLOPT_HEADER = true,

CURLOPT_RETURNTRANSFER = true,

CURLOPT_USERAGENT = $this-_SEND_USER_AGENT,

CURLOPT_REFERER = $this-_referer,

));

if ($this-_method=='GET') {

curl_setopt($ch,CURLOPT_HTTPGET,true);

} else {

if (is_array($params)) {

$QueryStr = http_build_query($params);

} else {

$QueryStr = $params;

}

curl_setopt($ch,CURLOPT_POST,true);

curl_setopt($ch,CURLOPT_POSTFIELDS,$QueryStr);

}

$fp = curl_exec($ch);

curl_close($ch);

if (!$fp) { return false; }

$i = 0; $length = strlen($fp);

// 读取 header

do{ $header.= substr($fp,$i,1); $i++; } while (!preg_match("/rnrn$/",$header));

// 遇到跳转,执行跟踪跳转

if ($this-_redirect($header)) { return true; }

// 读取内容

do {

$response.= substr($fp,$i,4096);

$i = $i + 4096;

} while ($length=$i);

unset($fp,$length,$i);

}

$this-_header = $header;

$this-_response = $response;

return true;

}

/**

* 跟踪跳转

*

* @param string $header

* @return bool

*/

function _redirect($header){

if (in_array($this-status($header),array(301,302))) {

if(preg_match("/Location:(.+)rn/i",$header,$regs)){

$this-connect(trim($regs[1]),$this-_method,$this-_timeout);

$this-send();

return true;

}

} else {

return false;

}

}

/**

* 取得请求的header

*

* @return string

*/

function header(){

return $this-_header;

}

/**

* 请求返回的html

*

* @return string

*/

function response(){

return $this-_response;

}

/**

* 返回状态

*

* @param string $header

* @return int

*/

function status($header=null){

if (empty($header)) {

$header = $this-_header;

}

if(preg_match("/(.+) (d+) (.+)rn/i",$header,$status)){

return $status[2];

} else {

return $this-_ERROR_CONNECT_FAILURE;

}

}

/**

* 解析url

*

* @param string $url

*/

function _parseURL($url){

$aUrl = parse_url($url);

$aUrl['query'] = isset($aUrl['query']) ? $aUrl['query'] : null;

$scheme = isset($_SERVER['HTTPS']) ? $_SERVER['HTTPS'] : null;

$this-_scheme = ($scheme=='off' || empty($scheme)) ? 'http' : 'https';

$this-_host = isset($aUrl['host']) ? $aUrl['host'] : null;

$this-_port = empty($aUrl['port']) ? 80 : (int)$aUrl['host'];

$this-_path = empty($aUrl['path']) ? '/' : (string)$aUrl['path'];

$this-_query = strlen($aUrl['query']) 0 ? '?'.$aUrl['query'] : null;

$this-_referer = $this-_scheme.'://'.$aUrl['host'];

}

}

$http = new Httplib('');

$http-send();

$body = $http-response();

echo $body;

php中头信息已被发送的错误怎么办

首先,你的文件是不是UTF-8编码,如果是那就需要专用工具去掉文件前面的三个字节(俗称BOM头),EDITPLUS等软件就能另外为无BOM的UTF-8编码文件。

如果不是UTF-8编码,或者已经去除了BOM,那么要保证header语句之前没有输出内容,文件的第一个?php前面没有任何空白、回车。或者在文件的最前面添加下面的内容:

?PHP

ob_start();

?

请问PHP 怎样请求时编写头信息并且还能获得返回的头信息?

一种使用fsockopen,所有的头信息全部手动连接成字符串即可。

一种使用curl,可以直接将头信息写成数组,调用curl_setopt设定头信息即可。

php中http请求头有什么内容,由什么组成

PHP中一般采用getallheaders来获取头部,但事实上,有些模式下是获取不到的(以前真没有注意过在fastcgi下这个函数不能用)

在PHP里,想要得到所有的HTTP请求头,可以使用getallheaders方法,不过此方法并不是在任何环境下都存在,比如说,你使用fastcgi方式运行PHP的话,就没有这个方法,所以说我们还需要考虑别的方法,幸运的是$_SERVER里有我们想要的东西,它里面键名以HTTP_开头的就是HTTP请求头:

$headers = array();

foreach ($_SERVER as $key = $value) {

if ('HTTP_' == substr($key, 0, 5)) {

$headers[str_replace('_', '-', substr($key, 5))] = $value;

}

}

代码很简单,需要说明的是RFC里明确指出了信息头的名字是不区分大小写的。

不过并不是所有的HTTP请求头都是以HTTP_开头的的键的形式存在与$_SERVER里,比如说Authorization,Content-Length,Content-Type就不是这样,所以说为了取得所有的HTTP请求头,还需要加上下面这段代码:

if (isset($_SERVER['PHP_AUTH_DIGEST'])) {

$header['AUTHORIZATION'] = $_SERVER['PHP_AUTH_DIGEST']);

} elseif (isset($_SERVER['PHP_AUTH_USER']) isset($_SERVER['PHP_AUTH_PW'])) {

$header['AUTHORIZATION'] = base64_encode($_SERVER['PHP_AUTH_USER'] . ':' . $_SERVER['PHP_AUTH_PW']));

}

if (isset($_SERVER['CONTENT_LENGTH'])) {

$header['CONTENT-LENGTH'] = $_SERVER['CONTENT_LENGTH'];

}

if (isset($_SERVER['CONTENT_TYPE'])) {

$header['CONTENT-TYPE'] = $_SERVER['CONTENT_TYPE'];

}

php如何获取http头部信息

var_dump(apache_request_headers());

//服务器是apache的话  (我没试过别的服务器能否用)

http_get_request_headers()//需安装 pecl_http 扩展

如何在php中获取curl请求的请求头信息及相应头信息

oCurl = curl_init();

// 设置请求头

$header[] = "Content-type: application/x-www-form-urlencoded";

$user_agent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.146 Safari/537.36";

curl_setopt($oCurl, CURLOPT_URL, $sUrl);

curl_setopt($oCurl, CURLOPT_HTTPHEADER,$header);

// 返回 response_header, 该选项非常重要,如果不为 true, 只会获得响应的正文

curl_setopt($oCurl, CURLOPT_HEADER, true);

// 是否不需要响应的正文,为了节省带宽及时间,在只需要响应头的情况下可以不要正文

curl_setopt($oCurl, CURLOPT_NOBODY, true);

// 使用上面定义的 ua

curl_setopt($oCurl, CURLOPT_USERAGENT,$user_agent);

curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1 );

// 不用 POST 方式请求, 意思就是通过 GET 请求

curl_setopt($oCurl, CURLOPT_POST, false);

$sContent = curl_exec($oCurl);

// 获得响应结果里的:头大小

$headerSize = curl_getinfo($oCurl, CURLINFO_HEADER_SIZE);

// 根据头大小去获取头信息内容

$header = substr($sContent, 0, $headerSize);

    

curl_close($oCurl);

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。