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
|
<?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: */*' , '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
|
<?php print_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 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
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
|
<?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: */*' , '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: *"); // 允许任意域名发起的跨域请求...
随机推荐
- 结构化查询语言-SQL
结构化查询语言(Structured Query Language)简称SQL(发音:/ˈes kjuː ˈel/ "S-Q-L"),是一种特殊目的的编程语言,是一种数据库查询和程 ...
- 多并发下 SimpleDateFormat 出现错误
private static String time = "2019-01-11 11:11:11"; private static long timestamp = 154717 ...
- sum特殊用法
在python中,list可以存储False和True a = [False] python的sum除了可以加数字,还可以计算列表中False,True的个数,默认是计算False个数 >> ...
- 实验1 c语言最基本内容
part 1 验证性内容 总结:经受了数组和结构体的双重折磨后,发现这部分好简单...现在没啥问题了... part 2 补全程序 1.判断奇偶 // 程序功能: // 要求用户从键盘输入一个整数 ...
- JavaScript -- 内置对象字符串
charAt和charCodeAt charAt语法: stringObject.charAt(index) 功能: 返回stringObject中index位置的字符. charCodeAt语法 s ...
- ps基础入门快捷方法总结
1. 快速打开文件 双击Photoshop的背景空白处(默认为灰色显示区域)即可打开选择文件的浏览窗口. 2. 随意更换画布颜色 选择油漆桶工具并按住Shift点击画布边缘,即可设置画布底色为当前选择 ...
- checkbox点击选中,再点击取消,并显示在文本框中
function checkItem(e,itemId) { var item = document.getElementById(itemId); var $items = $(item); if ...
- 用C#(ASP.Net)在Exchange Server环境下发送邮件
普通的邮件, 用System.Net.Mail 类 或 System.Web.Mail 类 处理即可, 但是Exchange Server 环境下, 这两个类起不了作用-------至少目前我看到的情 ...
- 【dp】P1077 摆花
基础DP题 题目描述 小明的花店新开张,为了吸引顾客,他想在花店的门口摆上一排花,共m盆.通过调查顾客的喜好,小明列出了顾客最喜欢的n种花,从1到n标号.为了在门口展出更多种花,规定第i种花不能超过a ...
- shopnc路由功能分析
项目核心文件 core/shopld.php if (!@include(BASE_DATA_PATH.'/config/config.ini.php')) exit('config.ini.php ...