PHP Set header information and the method to get the return header information _php tips

Source: Internet
Author: User
Tags curl sha1

This article describes the PHP setup header information and the method of obtaining the return header information. Share to everyone for your reference, specific as follows:

Set the request header information, we can use header function, can use Fsockopen, can use curl, etc., this article is mainly about using curl to set the header information, and get back the head information.

First, the requesting party sets its own header information, header.php

 <?php function Formatheader ($url, $myIp = null, $xml = NULL) {//extract URL $temp = Parse_url ($ur
 L); $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); Set header information to the place curl_setopt ($ch, Curlopt_header, 0);
Do not obtain return header information curl_setopt ($ch, Curlopt_timeout, 5);
curl_setopt ($ch, Curlopt_returntransfer, true);
$result = curl_exec ($ch); Var_dump ($result);?> 

Second, the requested party, obtain the head information, header2.php

<?php
Print_r ($_server);//header information contains most of the content is placed in the system variable
?>

Three, look at the results 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] => <address>apache/2.2.16 (Ubuntu) SERVER at localhost Port 80</ Address> ............ .........
)

The above few, we can obviously see, is the header information I set.

Iv. getting the header information returned

Copy Code code as follows:
curl_setopt ($ch, Curlopt_header, 1); Get Return header information

We set the Curlopt_header to 1, and in the results we get, we have this information in front of the array.

String (1239) "http/1.1 OK
Date:fri, May 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] => */*

Five, $_server part of the header information is not to get

Modify header.php

<?php function Formatheader ($url, $myIp = null, $xml = NULL) {//extract 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,//Modify 1" connection:close ");
return $header; } $xml = ' <?xml version= ' 1.0 ' encoding= ' utf-8 '?>//Modify 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);
Modified 3 $ch = Curl_init ();
curl_setopt ($ch, Curlopt_url, $interface); curl_setopt ($ch, Curlopt_httpheader, $header); Set header information to the place curl_setopt ($ch, Curlopt_header, 0);
Do not obtain return header information curl_setopt ($ch, Curlopt_timeout, 5);
curl_setopt ($ch, Curlopt_returntransfer, true);
$result = curl_exec ($ch);

 Var_dump ($result);?>

If so, the header2.php inside, the print $_server cannot print the XML in the head information. At this time, we add the following two lines behind the header2.php

$raw _post_data = file_get_contents (' php://input ', ' R ');
Var_dump ($raw _post_data);

This allows you to take the contents of the $xml and only take $xml content.

For more information on PHP-related content readers can view the site topics: "Introduction to PHP Basic Grammar", "PHP object-oriented programming Introductory Course" and "Php Curl Usage Summary"

I hope this article will help you with the PHP program design.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.