当前位置:首页 > PHP教程 > php高级应用 > 列表

PHP设置头信息及取得返回头信息的方法

发布:smiling 来源: PHP粉丝网  添加日期:2021-07-07 11:02:35 浏览: 评论:0 

本文实例讲述了PHP设置头信息及取得返回头信息的方法,分享给大家供大家参考,具体如下:

设置请求的头信息,我们可以用header函数,可以用fsockopen,可以用curl等,本文主要讲的是用curl来设置头信息,并取得返回后的头信息。

一、请求方设置自己的头信息,header.php

  1. <?php 
  2. function FormatHeader($url, $myIp = null,$xml = null) 
  3. { 
  4.  // 解悉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); //设置头信息的地方 
  26. curl_setopt($ch, CURLOPT_HEADER, 0); //不取得返回头信息 
  27. curl_setopt($ch, CURLOPT_TIMEOUT, 5); 
  28. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
  29. $result = curl_exec($ch); 
  30. var_dump($result); 
  31. ?> 

二、被请求方,取得头信息,header2.php

  1. <?php 
  2. print_r($_SERVER); //头信息里面有内容绝大部分是放在系统变量里面的 
  3. ?> 

三、看一下header.php请求的结果

  1. string(1045) "Array 
  2. ( 
  3. [HTTP_HOST] => localhost 
  4. [CONTENT_TYPE] => text/xml; charset=utf-8 
  5. [HTTP_ACCEPT] => */* 
  6. [HTTP_REFERER] => http://localhost/ 
  7. [HTTP_USER_AGENT] => Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; SV1) 
  8. [HTTP_X_FORWARDED_FOR] => 10.1.11.1 
  9. [CONTENT_LENGTH] => 380 
  10. [PATH] => /usr/local/bin:/usr/bin:/bin 
  11. [SERVER_SIGNATURE] => <address>Apache/2.2.16 (Ubuntu) Server at localhost Port 80</address> 
  12. 。。。。。。。。。。。。。。。。。。。。。。。。。。。。 
  13. ) 

上面那几个,我们可以明显看到,是我设置的头信息。

四、取得返回的头信息,代码如下:

curl_setopt($ch, CURLOPT_HEADER, 1); //取得返回头信息

我们把CURLOPT_HEADER设置成1,在取得的结果当中,显示数组的前面会有这些信息。

  1. string(1239) "HTTP/1.1 200 OK 
  2. Date: Fri, 27 May 2011 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] => */* 

五、$_SERVER部分头信息是拿不到的

修改一下header.php

  1. <?php 
  2. function FormatHeader($url, $myIp = null,$xml = null) 
  3. { 
  4.  // 解悉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, //修改1 
  17.  "Connection: Close" 
  18.  ); 
  19.  return $header; 
  20. } 
  21. $xml = '<?xml version="1.0" encoding="utf-8"?> //修改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); //修改3 
  35. $ch = curl_init(); 
  36. curl_setopt($ch, CURLOPT_URL, $interface); 
  37. curl_setopt($ch, CURLOPT_HTTPHEADER, $header); //设置头信息的地方 
  38. curl_setopt($ch, CURLOPT_HEADER, 0); //不取得返回头信息 
  39. curl_setopt($ch, CURLOPT_TIMEOUT, 5); 
  40. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
  41. $result = curl_exec($ch); 
  42. var_dump($result); 
  43. ?> 

如果这样的话,header2.php里面,打印$_SERVER不可能把头信息中的xml打印出来,这个时候,我们在header2.php后面加上以下二行

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

这样就可以取到$xml的内容,并且只会取$xml的内容。

Tags: PHP设置头信息 PHP返回头信息

分享到: