PHP设置头信息及取得返回头信息的方法


Posted in PHP onJanuary 25, 2016

本文实例讲述了PHP设置头信息及取得返回头信息的方法。分享给大家供大家参考,具体如下:

设置请求的头信息,我们可以用header函数,可以用fsockopen,可以用curl等,本文主要讲的是用curl来设置头信息,并取得返回后的头信息。

一、请求方设置自己的头信息,header.php

<?php
function FormatHeader($url, $myIp = null,$xml = null)
{
 // 解悉url
 $temp = parse_url($url);
 $query = isset($temp['query']) ? $temp['query'] : '';
 $path = isset($temp['path']) ? $temp['path'] : '/';
 $header = array (
 "POST {$path}?{$query} HTTP/1.1",
 "Host: {$temp['host']}",
 "Content-Type: text/xml; charset=utf-8",
 'Accept: */*',
 "Referer: http://{$temp['host']}/",
 'User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; SV1)',
 "X-Forwarded-For: {$myIp}",
 "Content-length: 380",
 "Connection: Close"
 );
 return $header;
}
$interface = 'http://localhost/test/header2.php';
$header = FormatHeader($interface,'10.1.11.1');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $interface);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header); //设置头信息的地方
curl_setopt($ch, CURLOPT_HEADER, 0); //不取得返回头信息
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
var_dump($result);
?>

二、被请求方,取得头信息,header2.php

<?php
print_r($_SERVER); //头信息里面有内容绝大部分是放在系统变量里面的
?>

三、看一下header.php请求的结果

string(1045) "Array
(
[HTTP_HOST] => localhost
[CONTENT_TYPE] => text/xml; charset=utf-8
[HTTP_ACCEPT] => */*
[HTTP_REFERER] => http://localhost/
[HTTP_USER_AGENT] => Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; SV1)
[HTTP_X_FORWARDED_FOR] => 10.1.11.1
[CONTENT_LENGTH] => 380
[PATH] => /usr/local/bin:/usr/bin:/bin
[SERVER_SIGNATURE] => <address>Apache/2.2.16 (Ubuntu) Server at localhost Port 80</address>
。。。。。。。。。。。。。。。。。。。。。。。。。。。。
)

上面那几个,我们可以明显看到,是我设置的头信息。

四、取得返回的头信息

curl_setopt($ch, CURLOPT_HEADER, 1); //取得返回头信息

我们把CURLOPT_HEADER设置成1,在取得的结果当中,显示数组的前面会有这些信息

string(1239) "HTTP/1.1 200 OK
Date: Fri, 27 May 2011 01:57:57 GMT
Server: Apache/2.2.16 (Ubuntu)
X-Powered-By: PHP/5.3.3-1ubuntu9.5
Vary: Accept-Encoding
Content-Length: 1045
Content-Type: text/html
Array
(
 [HTTP_HOST] => localhost
 [CONTENT_TYPE] => text/xml; charset=utf-8
 [HTTP_ACCEPT] => */*

五、$_SERVER部分头信息是拿不到的

修改一下header.php

<?php
function FormatHeader($url, $myIp = null,$xml = null)
{
 // 解悉url
 $temp = parse_url($url);
 $query = isset($temp['query']) ? $temp['query'] : '';
 $path = isset($temp['path']) ? $temp['path'] : '/';
 $header = array (
 "POST {$path}?{$query} HTTP/1.1",
 "Host: {$temp['host']}",
 "Content-Type: text/xml; charset=utf-8",
 'Accept: */*',
 "Referer: http://{$temp['host']}/",
 'User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; SV1)',
 "X-Forwarded-For: {$myIp}",
 "Content-length: " . strlen($xml) ."\r\n\r\n" .$xml, //修改1
 "Connection: Close"
 );
 return $header;
}
$xml = '<?xml version="1.0" encoding="utf-8"?> //修改2
 <profile>
 <sha1>adsfadsf</sha1>
 <user_id>asdfasdf</user_id>
 <album_id>asdf</album_id>
 <album_name>asdf</album_name>
 <tags>asdfasd</tags>
 <title>asdfasdf</title>
 <content>asdfadsf</content>
 <type>asdfasdf</type>
 <copyright>asdfasdf</copyright>
 </profile>';
$interface = 'http://localhost/test/header2.php';
$header = FormatHeader($interface,'10.1.11.1',$xml); //修改3
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $interface);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header); //设置头信息的地方
curl_setopt($ch, CURLOPT_HEADER, 0); //不取得返回头信息
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
var_dump($result);
?>

如果这样的话,header2.php里面,打印$_SERVER不可能把头信息中的xml打印出来。这个时候,我们在header2.php后面加上以下二行

$raw_post_data = file_get_contents('php://input', 'r');
var_dump($raw_post_data);

这样就可以取到$xml的内容,并且只会取$xml的内容。

希望本文所述对大家PHP程序设计有所帮助。

PHP 相关文章推荐
PHP安装攻略:常见问题解答(三)
Oct 09 PHP
PHP 柱状图实现代码
Dec 04 PHP
php实现无限级分类实现代码(递归方法)
Jan 01 PHP
smarty 缓存控制前的页面静态化原理
Mar 15 PHP
函数中使用require_once问题深入探讨 优雅的配置文件定义方法推荐
Jul 02 PHP
ThinkPHP整合百度Ueditor图文教程
Oct 21 PHP
PHP中使用substr()截取字符串出现中文乱码问题该怎么办
Oct 21 PHP
WordPress中给媒体文件添加分类和标签的PHP功能实现
Dec 31 PHP
PHP获取当前URL路径的处理方法(适用于多条件筛选列表)
Feb 10 PHP
php简单处理XML数据的方法示例
May 19 PHP
微信公众平台开发教程④ ThinkPHP框架下微信支付功能图文详解
Apr 10 PHP
PHP操作XML中XPath的应用示例
Jul 04 PHP
基于命令行执行带参数的php脚本并取得参数的方法
Jan 25 #PHP
crontab无法执行php的解决方法
Jan 25 #PHP
win7安装php框架Yii的方法
Jan 25 #PHP
php结合md5实现的加密解密方法
Jan 25 #PHP
PHP几个实用自定义函数小结
Jan 25 #PHP
php代码架构的八点注意事项
Jan 25 #PHP
详解js异步文件加载器
Jan 24 #PHP
You might like
基于mysql的bbs设计(四)
2006/10/09 PHP
PHP查询MySQL大量数据的时候内存占用分析
2011/07/22 PHP
PHP微信开发之查询微信精选文章
2016/06/23 PHP
php curl 模拟登录并获取数据实例详解
2016/12/22 PHP
Yii 访问 Gii(脚手架)时出现 403 错误
2018/06/06 PHP
PHP 扩展Memcached命令用法实例总结
2020/06/04 PHP
ImageFlow可鼠标控制图片滚动
2008/01/30 Javascript
js 日期转换成中文格式的函数
2009/07/07 Javascript
Wordpress ThickBox 点击图片显示下一张图的修改方法
2010/12/11 Javascript
浅谈Javascript中的Function与Object
2015/01/26 Javascript
JavaScript将一个数组插入到另一个数组的方法
2015/03/19 Javascript
js实现刷新iframe的方法汇总
2015/04/27 Javascript
jquery easyUI中ajax异步校验用户名
2016/08/19 Javascript
详解vue服务端渲染(SSR)初探
2017/06/19 Javascript
JS实现DOM删除节点操作示例
2018/04/04 Javascript
redux中间件之redux-thunk的具体使用
2018/04/17 Javascript
JS实现键值对遍历json数组功能示例
2018/05/30 Javascript
a标签调用js的方法总结
2019/09/05 Javascript
小程序自定义模板实现吸顶功能
2020/01/08 Javascript
vscode 插件开发 + vue的操作方法
2020/06/05 Javascript
[52:52]完美世界DOTA2联赛PWL S3 LBZS vs access 第一场 12.10
2020/12/13 DOTA
Python3实现将文件归档到zip文件及从zip文件中读取数据的方法
2015/05/22 Python
python实现冒泡排序算法的两种方法
2018/03/10 Python
Python中几种属性访问的区别与用法详解
2018/10/10 Python
python3读取图片并灰度化图片的四种方法(OpenCV、PIL.Image、TensorFlow方法)总结
2019/07/04 Python
Python3 批量扫描端口的例子
2019/07/25 Python
pd.DataFrame统计各列数值多少的实例
2019/12/05 Python
python 读写文件包含多种编码格式的解决方式
2019/12/20 Python
python实现滑雪者小游戏
2020/02/22 Python
酒店实习个人鉴定
2013/12/07 职场文书
销售工作岗位职责
2013/12/24 职场文书
咖啡书吧创业计划书
2014/01/13 职场文书
幼儿老师求职信
2014/06/30 职场文书
美术学专业求职信
2014/07/23 职场文书
工会经费申请报告
2015/05/15 职场文书
Nginx防盗链与服务优化配置的全过程
2022/01/18 Servers