Home > Backend Development > PHP Tutorial > How to set header information and get return header information in PHP, php returns_PHP tutorial

How to set header information and get return header information in PHP, php returns_PHP tutorial

WBOY
Release: 2016-07-12 08:59:56
Original
970 people have browsed it

How to set header information and get return header information in PHP, PHP returns

This article describes the method of setting header information and getting return header information in PHP. Share it with everyone for your reference, the details are as follows:

To set the header information of the request, we can use the header function, fsockopen, curl, etc. This article mainly talks about using curl to set the header information and obtain the returned header information.

1. The requester sets its own header information, header.php

<&#63;php
function FormatHeader($url, $myIp = null,$xml = null)
{
 // 解悉url
 $temp = parse_url($url);
 $query = isset($temp['query']) &#63; $temp['query'] : '';
 $path = isset($temp['path']) &#63; $temp['path'] : '/';
 $header = array (
 "POST {$path}&#63;{$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);
&#63;>

Copy after login

2. The requested party obtains the header information, header2.php

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

Copy after login

3. Take a look at the result of header.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>
。。。。。。。。。。。。。。。。。。。。。。。。。。。。
)

Copy after login

We can clearly see that the above ones are the header information I set.

4. Obtain the returned header information
Copy code The code is as follows: curl_setopt($ch, CURLOPT_HEADER, 1); //Get the return header information

We set CURLOPT_HEADER to 1. In the obtained results, there will be this information in front of the display array

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

Copy after login

5. The $_SERVER header information cannot be obtained

Modify header.php

<&#63;php
function FormatHeader($url, $myIp = null,$xml = null)
{
 // 解悉url
 $temp = parse_url($url);
 $query = isset($temp['query']) &#63; $temp['query'] : '';
 $path = isset($temp['path']) &#63; $temp['path'] : '/';
 $header = array (
 "POST {$path}&#63;{$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 = '<&#63;xml version="1.0" encoding="utf-8"&#63;> //修改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);
&#63;>

Copy after login

If this is the case, in header2.php, printing $_SERVER cannot print out the xml in the header information. At this time, we add the following two lines

after header2.php
$raw_post_data = file_get_contents('php://input', 'r');
var_dump($raw_post_data);

Copy after login

In this way, you can get the content of $xml, and only the content of $xml will be fetched.

Readers who are interested in more PHP-related content can check out the special topics of this site: "Introduction Tutorial on PHP Basic Syntax", "Introduction Tutorial on PHP Object-Oriented Programming" and "Summary of PHP Curl Usage"

I hope this article will be helpful to everyone in PHP programming.

Articles you may be interested in:

  • How PHP determines the image type by obtaining header information
  • PHP obtains the real address and response header information after the short link jumps Method
  • php method to view request header information and obtain remote image size sharing
  • Common http header settings for php header function
  • PHP Example parsing table for setting HTTP header using header function Header
  • Steps for PHP to obtain header information of http request
  • Crack anti-leech code by forging http header under PHP

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1095684.htmlTechArticleHow to set header information and get return header information in PHP, php returns This article describes how to set header information in PHP and get it Method to return header information. Share it with everyone for your reference, the details are as follows...
Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template