PHP Set header information, get Return header information

Source: Internet
Author: User
Tags sha1 vars

PHP Set header information, get Return header information

Zhang Ying published in 2011-05-27

Category: php

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.

One, the requester sets its own header information, header.php

View copy print?
  1. <?php
  2. function Formatheader ($url, $myIp = null,$xml = null)
  3. {
  4. //Extract URL
  5. $temp = parse_url ($url);
  6. $query = isset ($temp [' query '])?  $temp [' query ']: ';
  7. $path = isset ($temp [' path '])?  $temp [' path ']: '/';
  8. $header = Array (
  9. "POST {$path}?" {$query} http/1.1 ",
  10. "Host: {$temp [' Host ']}",
  11. "Content-type:text/xml; Charset=utf-8 ",
  12. ' Accept: */* ',
  13. "referer:http://{$temp [' Host ']}/',
  14. ' user-agent:mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; SV1) ',
  15. "x-forwarded-for: {$myIp}",
  16. "content-length:380",
  17. "Connection:close"
  18. );
  19. return $header;
  20. }
  21. $interface = ' http://localhost/test/header2.php ';
  22. $header = Formatheader ($interface,' 10.1.11.1 ');
  23. $ch = Curl_init ();
  24. curl_setopt ($ch, Curlopt_url, $interface);
  25. curl_setopt ($ch, Curlopt_httpheader, $header); //Where to set the header information
  26. curl_setopt ($ch, Curlopt_header, 0); //Do not get return header information
  27. curl_setopt ($ch, Curlopt_timeout, 5);
  28. curl_setopt ($ch, Curlopt_returntransfer, true);
  29. $result = curl_exec ($ch);
  30. Var_dump ($result);
  31. ?>

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

View copy print?
    1. <?php
    2. Print_r ($_server); //header information in most of the content is placed inside the system variables
    3. ?>

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 ones, we can clearly see, is the header information I set.

Four, get back the header information

View copy print?
    1. 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.

View copy print?
    1. String (1239) "http/1.1 OK
    2. Date:fri, 01:57:57 GMT
    3. server:apache/2.2.16 (Ubuntu)
    4. x-powered-by:php/5.3.3-1ubuntu9.5
    5. Vary:accept-encoding
    6. content-length:1045
    7. Content-type:text/html
    8. Array
    9. (
    10. [Http_host] = localhost
    11. [Content_Type] = Text/xml; Charset=utf-8
    12. [Http_accept] = */*
    13. 。。。。。。。。。。。。。。。。

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

Modify the header.php

View copy print?
  1. <?php
  2. function Formatheader ($url, $myIp = null,$xml = null)
  3. {
  4. //Extract URL
  5. $temp = parse_url ($url);
  6. $query = isset ($temp [' query '])?  $temp [' query ']: ';
  7. $path = isset ($temp [' path '])?  $temp [' path ']: '/';
  8. $header = Array (
  9. "POST {$path}?" {$query} http/1.1 ",
  10. "Host: {$temp [' Host ']}",
  11. "Content-type:text/xml; Charset=utf-8 ",
  12. ' Accept: */* ',
  13. "referer:http://{$temp [' Host ']}/',
  14. ' user-agent:mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; SV1) ',
  15. "x-forwarded-for: {$myIp}",
  16. "Content-length:". strlen ($xml)." \r\n\r\n ". $xml, //modify 1
  17. "Connection:close"
  18. );
  19. return $header;
  20. }
  21. $xml = ' <?xml version= ' 1.0 "encoding=" Utf-8 "?>//Modify 2
  22. <profile>
  23. <sha1>adsfadsf</sha1>
  24. <user_id>asdfasdf</user_id>
  25. <album_id>asdf</album_id>
  26. <album_name>asdf</album_name>
  27. <tags>asdfasd</tags>
  28. <title>asdfasdf</title>
  29. <content>asdfadsf</content>
  30. <type>asdfasdf</type>
  31. <copyright>asdfasdf</copyright>
  32. </profile> ';
  33. $interface = ' http://localhost/test/header2.php ';
  34. $header = Formatheader ($interface,' 10.1.11.1 ',$xml); //Modify 3
  35. $ch = Curl_init ();
  36. curl_setopt ($ch, Curlopt_url, $interface);
  37. curl_setopt ($ch, Curlopt_httpheader, $header); //Where to set the header information
  38. curl_setopt ($ch, Curlopt_header, 0); //Do not get return header information
  39. curl_setopt ($ch, Curlopt_timeout, 5);
  40. curl_setopt ($ch, Curlopt_returntransfer, true);
  41. $result = curl_exec ($ch);
  42. Var_dump ($result);
  43. ?>

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.

View copy print?
    1. $raw _post_data = file_get_contents (' php://input ', ' R ');
    2. Var_dump ($raw _post_data);

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

PHP Set header information, get Return header information

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.