PHP Set header information and get the return header information method, PHP return _php tutorial

Source: Internet
Author: User
Tags sha1

PHP to set the header information and get the return header information method, PHP return


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

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

First, the requester set its own header information, header.php

<?phpfunction 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: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 where curl_setopt ($ch, Curlopt_header, 0); Do not get 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

<?phpprint_r ($_server); Most of the information inside the header is placed in the system variable?>

Third, 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] = apache/2.2.16 (Ubuntu) Server at localhost Port 80 ..... .............. )

The above ones, we can clearly see, is the header information I set.

Iv. obtaining the return header information
Copy the 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 Okdate:fri, 01:57:57 gmtserver:apache/2.2.16 (Ubuntu) X-powered-by:php/5.3.3-1ubu Ntu9.5vary:accept-encodingcontent-length:1045content-type:text/htmlarray ([http_host] = localhost [CONTENT_ TYPE] = Text/xml; Charset=utf-8 [http_accept] = */*

Five, $_server part of the head information is not get

Modify the header.php

<?phpfunction Formatheader ($url, $myIp = null, $xml = NULL) {//extract URL $temp = Parse_url ($url); $query = Isset ($temp [' Que Ry '])? $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
 
   
   
    adsfadsf
    
   
    asdfasdf
    asdf asdf 
   
    asdfasd
    
  
  
 
   Asdfasdf 
 
   
    ASDFADSF
    
   
    ASDFASDF
    
   
    asdfasdf
     
  
  

  
 '; $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 where curl_setopt ($ch, Curlopt_header, 0); Do not get return header information curl_setopt ($ch, Curlopt_timeout, 5); curl_setopt ($ch, Curlopt_returntransfer, true); $result = Curl_exec ($ CH); Var_dump ($result);? >

If so, header2.php inside, print $_server cannot print out the XML in the head information. At this point, we'll add the following two lines to the back of header2.php.

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

This allows the content to be $xml and only $xml content is taken.

More readers interested in PHP related content can view the topic: PHP Basic Grammar Introductory tutorial, PHP Object-oriented Programming primer tutorial and PHP Curl Usage Summary

I hope this article is helpful to you in PHP programming.

Articles you may be interested in:

    • Php method for judging image types by getting header information
    • How to get the real address and response header information of a short link jump after PHP
    • PHP View request header information to get remote picture size sharing method
    • Common HTTP header settings for PHP header functions
    • PHP uses the header function to set an example parsing header for HTTP headers
    • PHP Get header Information implementation steps for HTTP request
    • Code to crack an anti-theft chain by forging HTTP headers under PHP

http://www.bkjia.com/PHPjc/1095684.html www.bkjia.com true http://www.bkjia.com/PHPjc/1095684.html techarticle PHP to set the header information and the method to get the return header information, PHP returns the example of this article describes the PHP configuration header information and the method to obtain the return header information. Share to everyone for your reference, as follows ...

  • Related Article

    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.