curl 设置头部
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43 <?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://wang.com/curl/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, 1); //不取得返回头信息
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
var_dump($result);
?>
<?php
/**
* Created by PhpStorm.
* User: brady
* Date: 2017/12/11
* Time: 19:50
*/ //print_r($_SERVER); //头信息里面有内容绝大部分是放在系统变量里面的
$raw_post_data = file_get_contents('php://input', 'r');
var_dump($raw_post_data);
参考
http://www.jb51.net/article/78609.htm
本文实例讲述了PHP设置头信息及取得返回头信息的方法。分享给大家供大家参考,具体如下:
设置请求的头信息,我们可以用header函数,可以用fsockopen,可以用curl等,本文主要讲的是用curl来设置头信息,并取得返回后的头信息。
一、请求方设置自己的头信息,header.php
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
<?phpfunction 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: */*', '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;}$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
|
1
2
3
|
<?phpprint_r($_SERVER); //头信息里面有内容绝大部分是放在系统变量里面的?> |
三、看一下header.php请求的结果
|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
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>。。。。。。。。。。。。。。。。。。。。。。。。。。。。) |
上面那几个,我们可以明显看到,是我设置的头信息。
四、取得返回的头信息
我们把CURLOPT_HEADER设置成1,在取得的结果当中,显示数组的前面会有这些信息
|
1
2
3
4
5
6
7
8
9
10
11
12
|
string(1239) "HTTP/1.1 200 OKDate: Fri, 27 May 2011 01:57:57 GMTServer: Apache/2.2.16 (Ubuntu)X-Powered-By: PHP/5.3.3-1ubuntu9.5Vary: Accept-EncodingContent-Length: 1045Content-Type: text/htmlArray( [HTTP_HOST] => localhost [CONTENT_TYPE] => text/xml; charset=utf-8 [HTTP_ACCEPT] => */* |
五、$_SERVER部分头信息是拿不到的
修改一下header.php
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
<?phpfunction 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: */*', '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>';$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后面加上以下二行
|
1
2
|
var_dump($raw_post_data); |
这样就可以取到$xml的内容,并且只会取$xml的内容。
curl 设置头部的更多相关文章
- PHP header函数设置http报文头(设置头部域)
PHP HTTP 简介: HTTP 函数允许您在其他输出被发送之前,对由 Web 服务器发送到浏览器的信息进行操作. PHP 5 HTTP 函数:header() 向客户端发送原始的 HTTP ...
- php使用curl设置超时的重要性
原文:http://phpquan.com/lamp/php/php-curl-timeout/ 网站登录不了,原因是没有可用的 PHP 子进程来响应新的请求了.这可能是是由于PHP-curl 没有 ...
- [转]php curl 设置host curl_setopt CURLOPT_HTTPHEADER 指定host
From : http://digdeeply.org/archives/10132139.html 我们在开发测试时,有时web服务器会绑定一个域名,但是因为dns是无法解析的,我们需要设置host ...
- php之curl设置超时实例
本文实例讲述了php中curl超时设置方法.分享给大家供大家参考.具体实现方法如下: 访问HTTP方式很多,可以使用curl, socket, file_get_contents() 等方法. 在访问 ...
- PHP——curl设置请求头需要注意哪些
前言 在设置这个请求头上踩了一些坑,此文记录下. 步骤 设置请求头 curl_setopt($ch, CURLOPT_HTTPHEADER, $header); 请求头写法 一定不要忘记:不然进行请求 ...
- curl 返回头部和正文
头部string(195) "HTTP/1.1 200 OK Server: openresty/1.7.7.1 Date: Wed, 05 Sep 2018 13:18:33 GMT Co ...
- Linux下CURL设置请求超时时间
使用CURL时,有两个超时时间:一个是连接超时时间,另一个是数据传输的最大允许时间. 连接超时时间用--connect-timeout参数来指定,数据传输的最大允许时间用-m参数来指定. 例如: cu ...
- curl 设置超时时间
使用CURL时,有两个超时时间:一个是连接超时时间,另一个是数据传输的最大允许时间.连接超时时间用--connect-timeout参数来指定,数据传输的最大允许时间用-m参数来指定. curl -- ...
- php设置头部让任何域名请求
header("Access-Control-Allow-Origin: *"); // 允许任意域名发起的跨域请求...
随机推荐
- [论文理解]Region-Based Convolutional Networks for Accurate Object Detection and Segmentation
Region-Based Convolutional Networks for Accurate Object Detection and Segmentation 概括 这是一篇2016年的目标检测 ...
- 【转】IOS开发—IOS 8 中设置applicationIconBadgeNumber和消息推送
在IOS7中设置applicationIconBadgeNumber不会有什么问题,但是直接在IOS8中设置applicationIconBadgeNumber会报错 因为在IOS8中要想设置appl ...
- pycharm创建工程的两种形式:virtualenv环境和系统默认编译器
转自:http://swiftlet.net/archives/3151 pycharm创建工程的时候可以选择编译器,如下图所示: 上图表示创建工程有两种方式:第一种是利用:virtualenv,第二 ...
- 用python Image读图
https://www.cnblogs.com/kongzhagen/p/6295925.html import os name = [] with open('/media/hdc/xing/Dee ...
- LibreOffice Writer Comment
选择文字段后输入:Ctrl+Alt+C可以插入评论,但会出现: unknown author 解决方法:Tools>Options>LibreOffice>User Data. Fi ...
- Java基础 匿名内部类 异常 多线程 集合面试题
匿名内部类:没有名字的内部类.就是内部类的简化形式.一般只用一次就可以用这种形式.匿名内部类其实就是一个匿名子类对象.想要定义匿名内部类:需要前提,内部类必须继承一个类或者实现接口. 匿名内部类的格式 ...
- 【转】再谈 最速下降法/梯度法/Steepest Descent
转载请注明出处:http://www.codelast.com/ 最速下降法(又称梯度法,或Steepest Descent),是无约束最优化领域中最简单的算法,单独就这种算法来看,属于早就“过时”了 ...
- 【转】Windows 邮件槽(MailSlot)
Windows 邮件槽(MailSlot) 来自<Windows网络编程第二版 中文版> 优点:通过网络,将一条消息广播给一台或多台计算机. 缺点:只允许从客户机到服务器,建立一种不可 ...
- VB6 代码编辑页面添加支持滚轮模式
VB6 中的代码编辑页面默认是不支持滚轮模式的,这让在编辑代码时的体验很是不爽. 但在64位win10系统进行加载配置时,可能会出现问题,可用如下方法解决: http://download.micro ...
- mariadb多源主从复制错误跳过.md
mysql 的主从错误跳过和mariadb的多源主从复制错误跳过操作不同,请注意: 更改会话的default_master_connection变量 STOP SLAVE 'slave_account ...