PHP sets the header information and obtains the returned header information. PHP sets the header information and obtains the returned header information. php returns this example to describe how PHP sets the header information and obtains the returned header information. For your reference, the following describes how to set the header information and obtain the returned header information in PHP.
This article describes how to set the header information and obtain the returned header information in PHP. We will share this with you for your reference. The details are as follows:
Set the request header information. we can use the header function, fsockopen, curl, and so on. This article mainly describes how to use curl to set the header information, obtain the returned header information.
1. the requester sets its own header information, header. php
<? Phpfunction FormatHeader ($ url, $ myIp = null, $ xml = null) {// Obtain the 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 = c Url_init (); curl_setopt ($ ch, CURLOPT_URL, $ interface); curl_setopt ($ ch, CURLOPT_HTTPHEADER, $ header); // curl_setopt ($ ch, CURLOPT_HEADER, 0); // do not get the returned header information curl_setopt ($ ch, CURLOPT_TIMEOUT, 5); curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, true); $ result = curl_exec ($ ch ); var_dump ($ result);?>
2. the requested party obtains the header information, header2.php
<? Phpprint_r ($ _ SERVER); // most of the content in the header information is stored in system variables?>
3. check the result of the 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] => Apache/2.2.16 (Ubuntu) Server at localhost Port 80。。。。。。。。。。。。。。。。。。。。。。。。。。。。)
We can see the headers I set.
4. get the returned header information
The code is as follows: curl_setopt ($ ch, CURLOPT_HEADER, 1); // Get the returned header information
We set CURLOPT_HEADER to 1. in the obtained result, this information is displayed before the array.
string(1239) "HTTP/1.1 200 OKDate: Fri, 27 May 2011 01:57:57 GMTServer: Apache/2.2.16 (Ubuntu)X-Powered-By: PHP/5.3.3-1ubuntu9.5Vary: Accept-EncodingContent-Length: 1045Content-Type: text/htmlArray( [HTTP_HOST] => localhost [CONTENT_TYPE] => text/xml; charset=utf-8 [HTTP_ACCEPT] => */*
5. some header information of $ _ SERVER cannot be obtained
Modify header. php
<? Phpfunction FormatHeader ($ url, $ myIp = null, $ xml = null) {// Obtain the 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 ". $ xml, // modify 1 "Connection: Close"); return $ header;} $ xml = '<? Xml version = "1.0" encoding = "UTF-8"?> // Modify 2
Adsfadsf
Asdfasdf
AsdfAsdf
Asdfasd
Asdfasdf
Asdfadsf
Asdfasdf
Asdfasdf
'; $ Interface = 'http: // localhost/test/header2.php'; $ header = FormatHeader ($ interface, '10. 1.11.1 ', $ xml); // modify 3 $ ch = curl_init (); curl_setopt ($ ch, CURLOPT_URL, $ interface); curl_setopt ($ ch, CURLOPT_HTTPHEADER, $ header ); // Set the header information curl_setopt ($ ch, CURLOPT_HEADER, 0); // do not obtain the returned header information curl_setopt ($ ch, CURLOPT_TIMEOUT, 5); curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, true); $ result = curl_exec ($ ch); var_dump ($ result );? >
In this case, in header2.php, printing $ _ SERVER cannot print the xml in the header information. At this time, we add the following two lines behind header2.php:
$raw_post_data = file_get_contents('php://input', 'r');var_dump($raw_post_data);
In this way, you can get the content of $ xml and only get the content of $ xml.