curl 設置頭部
阿新 • • 發佈:2017-12-11
face ora 函數 $path 參考 test fad help contents
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: */*‘ ,
"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
?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: */*‘ ,
"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後面加上以下二行
?1 2 |
$raw_post_data = file_get_contents ( ‘php://input‘ , ‘r‘ );
var_dump( $raw_post_data );
|
這樣就可以取到$xml的內容,並且只會取$xml的內容。
curl 設置頭部