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 保留小数点
Apr 21 PHP
Discuz 模板语句分析及知识技巧
Aug 21 PHP
php5.3 废弃函数小结
May 16 PHP
使用swoole扩展php websocket示例
Feb 13 PHP
PHP版微信公众平台红包API
Apr 02 PHP
php实现微信公众号无限群发
Oct 11 PHP
PHP简单的MVC框架实现方法
Dec 01 PHP
PHP导出带样式的Excel示例代码
Aug 28 PHP
浅谈PHP安全防护之Web攻击
Jan 03 PHP
PHP实现移除数组中为空或为某值元素的方法
Jan 07 PHP
thinkPHP5.0框架URL访问方法详解
Mar 18 PHP
PHP随机生成中文段落示例【测试网站内容时使用】
Apr 26 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 引用是个坏习惯
2010/03/12 PHP
php的日期处理函数及uchome的function_coomon中日期处理函数的研究
2011/01/12 PHP
在php中判断一个请求是ajax请求还是普通请求的方法
2011/06/28 PHP
php在文件指定行中写入代码的方法
2012/05/23 PHP
PHP读取数据库并按照中文名称进行排序实现代码
2013/01/29 PHP
WordPress中使主题支持小工具以及添加插件启用函数
2015/12/22 PHP
PHP单例模式是什么 php实现单例模式的方法
2016/05/14 PHP
jQuery + Flex 通过拖拽方式动态改变图片的代码
2011/08/03 Javascript
jquery结合html实现中英文页面切换
2016/11/29 Javascript
详解nodejs微信公众号开发——5.素材管理接口
2017/04/11 NodeJs
详解如何使用 vue-cli 开发多页应用
2017/12/16 Javascript
使用vue-cli打包过程中的步骤以及问题的解决
2018/05/08 Javascript
ES6知识点整理之函数对象参数默认值及其解构应用示例
2019/04/17 Javascript
Vue实现导航栏点击当前标签变色功能
2020/08/19 Javascript
基于layui实现高级搜索(筛选)功能
2019/07/26 Javascript
vue-cli创建的项目中的gitHooks原理解析
2020/02/14 Javascript
vue组件开发之tab切换组件使用详解
2020/08/21 Javascript
Vue 401配合Vuex防止多次弹框的案例
2020/11/11 Javascript
如何使用 JavaScript 操作浏览器历史记录 API
2020/11/24 Javascript
使用python实现抓取腾讯视频所有电影的爬虫
2019/04/15 Python
Python面向对象封装操作案例详解 II
2020/01/02 Python
Python requests获取网页常用方法解析
2020/02/20 Python
PyQt5-QDateEdit的简单使用操作
2020/07/12 Python
python编程的核心知识点总结
2021/02/08 Python
CSS3 重置iphone浏览器按钮input,select等表单元素的默认样式
2014/10/11 HTML / CSS
详解canvas在圆弧周围绘制文本的两种写法
2018/05/22 HTML / CSS
英国知名美妆护肤在线商城:Zest Beauty
2018/04/24 全球购物
日本索尼音乐商店:Sony Music Shop
2018/07/17 全球购物
阿玛尼美妆俄罗斯官网:Giorgio Armani Beauty RU
2020/07/19 全球购物
巴西最大的巴士票务门户:Quero Passagem
2020/11/21 全球购物
国际政治个人自荐信范文
2013/11/26 职场文书
禁毒宣传活动总结
2014/08/26 职场文书
2016婚礼主持词开场白
2015/11/24 职场文书
2016年感恩教师节校园广播稿
2015/12/18 职场文书
公司周年庆寄语
2019/06/21 职场文书
《飘》英文读后感五篇
2019/10/11 职场文书