>  기사  >  백엔드 개발  >  PHP에서 헤더 정보를 설정하고 헤더 정보를 반환받는 방법

PHP에서 헤더 정보를 설정하고 헤더 정보를 반환받는 방법

不言
不言원래의
2018-05-14 13:35:204620검색

이 글에서는 주로 PHP에서 헤더 정보를 설정하고 반환된 헤더 정보를 얻는 방법을 소개합니다. PHP의 컬 기반 헤더 정보 운용 기법을 예시와 함께 분석합니다. 필요한 친구들이 참고할 수 있습니다

이 글에서는 헤더 설정 방법을 설명합니다. PHP의 정보와 이를 얻는 방법. 헤더 정보를 반환하는 Get 메서드. 참고할 수 있도록 모든 사람과 공유하세요. 세부 사항은 다음과 같습니다.

요청의 헤더 정보를 설정하려면 헤더 기능, fsockopen, 컬 등을 사용할 수 있습니다. 이 기사에서는 주로 사용에 대해 설명합니다. 컬을 사용하여 헤더 정보를 설정하고 반환된 헤더 정보를 가져옵니다.

1. 요청자는 자체 헤더 정보인 header.php

<?php
function FormatHeader($url, $myIp = null,$xml = null)
{
 // 解悉url
 $temp = parse_url($url);
 $query = isset($temp[&#39;query&#39;]) ? $temp[&#39;query&#39;] : &#39;&#39;;
 $path = isset($temp[&#39;path&#39;]) ? $temp[&#39;path&#39;] : &#39;/&#39;;
 $header = array (
 "POST {$path}?{$query} HTTP/1.1",
 "Host: {$temp[&#39;host&#39;]}",
 "Content-Type: text/xml; charset=utf-8",
 &#39;Accept: */*&#39;,
 "Referer: http://{$temp[&#39;host&#39;]}/",
 &#39;User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; SV1)&#39;,
 "X-Forwarded-For: {$myIp}",
 "Content-length: 380",
 "Connection: Close"
 );
 return $header;
}
$interface = &#39;http://localhost/test/header2.php&#39;;
$header = FormatHeader($interface,&#39;10.1.11.1&#39;);
$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);
?>

2를 설정합니다. 요청한 당사자는 헤더 정보인 header2.php

<?php
print_r($_SERVER); //头信息里面有内容绝大部分是放在系统变量里面的
?>

3을 확인합니다. php request

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>
。。。。。。。。。。。。。。。。。。。。。。。。。。。。
)

위에서 확실히 알 수 있는 것은 제가 설정한 헤더 정보입니다.

4. 반환된 헤더 정보 가져오기

코드 복사 코드는 다음과 같습니다.

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] => */*

5 , 헤더 정보 중 $_SERVER 부분이 나오지 않습니다

header.php를 수정하세요

<?php
function FormatHeader($url, $myIp = null,$xml = null)
{
 // 解悉url
 $temp = parse_url($url);
 $query = isset($temp[&#39;query&#39;]) ? $temp[&#39;query&#39;] : &#39;&#39;;
 $path = isset($temp[&#39;path&#39;]) ? $temp[&#39;path&#39;] : &#39;/&#39;;
 $header = array (
 "POST {$path}?{$query} HTTP/1.1",
 "Host: {$temp[&#39;host&#39;]}",
 "Content-Type: text/xml; charset=utf-8",
 &#39;Accept: */*&#39;,
 "Referer: http://{$temp[&#39;host&#39;]}/",
 &#39;User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; SV1)&#39;,
 "X-Forwarded-For: {$myIp}",
 "Content-length: " . strlen($xml) ."\r\n\r\n" .$xml, //修改1
 "Connection: Close"
 );
 return $header;
}
$xml = &#39;<?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>&#39;;
$interface = &#39;http://localhost/test/header2.php&#39;;
$header = FormatHeader($interface,&#39;10.1.11.1&#39;,$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이 출력되지 않습니다. 이때 $xml의 내용을 가져오도록 header2.php

$raw_post_data = file_get_contents(&#39;php://input&#39;, &#39;r&#39;);
var_dump($raw_post_data);

뒤에 다음 두 줄을 추가하고, $xml의 내용만 가져오도록 한다.

관련 권장사항:

PHP가 쿠키의 HTTPONLY 속성을 설정하는 방법에 대한 자세한 설명

php용 서버(Apache/Nginx) 환경 변수 설정



위 내용은 PHP에서 헤더 정보를 설정하고 헤더 정보를 반환받는 방법의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.