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 相关文章推荐
JAVA/JSP学习系列之七
Oct 09 PHP
BBS(php &amp; mysql)完整版(一)
Oct 09 PHP
php面向对象全攻略 (十五) 多态的应用
Sep 30 PHP
php 高效率写法 推荐
Feb 21 PHP
php生成二维码时出现中文乱码的解决方法
Dec 18 PHP
php遍历删除整个目录及文件的方法
Mar 13 PHP
PHP开发中常用的十个代码样例
Feb 02 PHP
CI框架扩展系统核心类的方法分析
May 23 PHP
PHP中list方法用法示例
Dec 01 PHP
Yii实现复选框批量操作实例代码
Mar 15 PHP
深入浅析PHP的session反序列化漏洞问题
Jun 15 PHP
PHP配合fiddler抓包抓取微信指数小程序数据的实现方法分析
Jan 02 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
在PHP中使用与Perl兼容的正则表达式
2006/11/26 PHP
CodeIgniter实现更改view文件夹路径的方法
2014/07/04 PHP
PHP环境搭建(php+Apache+mysql)
2016/11/14 PHP
Laravel框架实现model层的增删改查(CURD)操作示例
2018/05/12 PHP
JS应用之禁止抓屏、复制、打印
2008/02/21 Javascript
Jquery cookie操作代码
2010/03/14 Javascript
基于Jquery实现表格动态分页实现代码
2011/06/21 Javascript
理解JSON:3分钟课程
2011/10/28 Javascript
jQuery中closest()函数用法实例
2015/01/07 Javascript
Vue.js 通过jQuery ajax获取数据实现更新后重新渲染页面的方法
2018/08/09 jQuery
vue实现简单的日历效果
2020/09/24 Javascript
一文看懂如何简单实现节流函数和防抖函数
2019/09/05 Javascript
理解Proxy及使用Proxy实现vue数据双向绑定操作
2020/07/18 Javascript
解决vue prop传值default属性如何使用,为何不生效的问题
2020/09/21 Javascript
element中Steps步骤条和Tabs标签页关联的解决
2020/12/08 Javascript
Flask和Django框架中自定义模型类的表名、父类相关问题分析
2018/07/19 Python
Python实现对字典分别按键(key)和值(value)进行排序的方法分析
2018/12/19 Python
ActiveMQ:使用Python访问ActiveMQ的方法
2019/01/30 Python
Ubuntu18.04下python版本完美切换的解决方法
2019/06/14 Python
Python OrderedDict的使用案例解析
2019/10/25 Python
Django中modelform组件实例用法总结
2020/02/10 Python
解决python-docx打包之后找不到default.docx的问题
2020/02/13 Python
python实现FTP循环上传文件
2020/03/20 Python
Python+Appium实现自动化测试的使用步骤
2020/03/24 Python
Python 字节流,字符串,十六进制相互转换实例(binascii,bytes)
2020/05/11 Python
python 如何将office文件转换为PDF
2020/09/22 Python
html5 跨文档消息传输示例探讨
2013/04/01 HTML / CSS
MySQL面试题
2014/01/12 面试题
初中英语教学反思
2014/01/25 职场文书
森林防火标语
2014/06/23 职场文书
机关党员进社区活动总结
2014/07/05 职场文书
成都人事代理协议书
2014/10/25 职场文书
二年级上册数学教学计划
2015/01/20 职场文书
Nginx 反向代理解决跨域问题多种情况分析
2022/01/18 Servers
mongoDB数据库索引快速入门指南
2022/03/23 MongoDB
GoFrame gredis缓存DoVar Conn连接对象 自动序列化GoFrame gredisDo/DoVar方法Conn连接对象自动序列化/反序列化总结
2022/06/14 Golang