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: *"); // 允许任意域名发起的跨域请求...
随机推荐
- 洛谷 P2598 [ZJOI2009]狼和羊的故事
题目描述 “狼爱上羊啊爱的疯狂,谁让他们真爱了一场:狼爱上羊啊并不荒唐,他们说有爱就有方向......” Orez听到这首歌,心想:狼和羊如此和谐,为什么不尝试羊狼合养呢?说干就干! Orez的羊狼圈 ...
- Java异常之RuntimeException
人生不如意十有八九.在打Core Java里面的例子的时候总是一遍就过,但是实际上只要是自己想着动手去打造自己想要的东西,异常的状况也是十有八九的. 在Java中会使用异常处理的错误捕获机制处理这些异 ...
- C基础的练习集及测试答案(31-39)
31.读懂以下程序,说明程序的功能#include<stdio.h>int main(){ int m,n,r,m1,m2; printf("请输入2个正整数:"); ...
- 后台登录验证(Tokens/JSON Web Tokens(JWT) 的认证机制)
sessionid不支持跨域,浏览器也不能禁止cookie(禁止以后sessionid还有什么用) 单点登录问题,即时SessionID一样,也无法跨域获取到数据 占坑
- Android(java)学习笔记107:Relativelayout相对布局
1. Relativelayout相对布局案例: 我们看看案例代码,自己心领神会: <?xml version="1.0" encoding="utf-8" ...
- NOIP模拟赛 czy的后宫4
czy的后宫4 [问题描述] czy有很多妹子,妹子虽然数量很多,但是质量不容乐观,她们的美丽值全部为负数(喜闻乐见). czy每天都要带N个妹子到机房,她们都有一个独一无二的美丽值,美丽值为-1到- ...
- 【思维题 kmp 构造】bzoj4974: [Lydsy1708月赛]字符串大师
字符串思博题这一块还是有点薄弱啊. Description 一个串T是S的循环节,当且仅当存在正整数k,使得S是T^k(即T重复k次)的前缀,比如abcd是abcdabcdab的循环节 .给定一个长度 ...
- 03IO端口寻址和访问控制方式
1. I/O端口和寻址 CPU 为了访问 I/O 接口控制器或者控制卡上的数据和状态信息,需要首先指定他们的地址.这种地址就称为I/O端口地址或简称端口.通常一个 I/O 控制器包含访问数据的数据端口 ...
- python入门:输出1-10以内除去7的所有数(经典)
#!/usr/bin/env python # -*-coding:utf-8 -*- #输出1-10以内除去7的所有数(经典) """ 给kaishi赋值为1,whil ...
- 流程控制之while循环for循环
流程控制之while循环1.什么是循环 循环就是重复做某件事2.为什么要有循环 为了让计算机能够具备人重复做某件事的能力3.如何用循环 while语法: while 条件: code1 code2 c ...