zoukankan      html  css  js  c++  java
  • PHP设置头信息,取得返回头信息

    php 设置头信息,取得返回头信息

    张映 发表于 2011-05-27

    分类目录: 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.   
    9.  $header = array (  
    10.  "POST {$path}?{$query} HTTP/1.1",  
    11.  "Host: {$temp['host']}",  
    12.  "Content-Type: text/xml; charset=utf-8",  
    13.  'Accept: */*',  
    14.  "Referer: http://{$temp['host']}/",  
    15.  'User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; SV1)',  
    16.  "X-Forwarded-For: {$myIp}",  
    17.  "Content-length: 380",  
    18.  "Connection: Close"  
    19.  );  
    20.   
    21.  return $header;  
    22. }   
    23.   
    24. $interface = 'http://localhost/test/header2.php';  
    25. $header = FormatHeader($interface,'10.1.11.1');  
    26.   
    27. $ch = curl_init();  
    28. curl_setopt($ch, CURLOPT_URL, $interface);  
    29. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);  //设置头信息的地方  
    30. curl_setopt($ch, CURLOPT_HEADER, 0);    //不取得返回头信息  
    31. curl_setopt($ch, CURLOPT_TIMEOUT, 5);  
    32. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  
    33. $result = curl_exec($ch);  
    34.   
    35. var_dump($result);  
    36. ?>  

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

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

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

    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>
    。。。。。。。。。。。。。。。。。。。。。。。。。。。。
    )

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

    四,取得返回的头信息

    1. 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.   
    9. Array  
    10. (  
    11.  [HTTP_HOST] => localhost  
    12.  [CONTENT_TYPE] => text/xml; charset=utf-8  
    13.  [HTTP_ACCEPT] => */*  
    14.  。。。。。。。。。。。。。。。。  

    五,$_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.   
    9.  $header = array (  
    10.  "POST {$path}?{$query} HTTP/1.1",  
    11.  "Host: {$temp['host']}",  
    12.  "Content-Type: text/xml; charset=utf-8",  
    13.  'Accept: */*',  
    14.  "Referer: http://{$temp['host']}/",  
    15.  'User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; SV1)',  
    16.  "X-Forwarded-For: {$myIp}",  
    17.  "Content-length: " . strlen($xml) ." " .$xml,  //修改1  
    18.  "Connection: Close"  
    19.  );  
    20.   
    21.  return $header;  
    22. }   
    23.   
    24. $xml = '<?xml version="1.0" encoding="utf-8"?>   //修改2 
    25.  <profile> 
    26.  <sha1>adsfadsf</sha1> 
    27.  <user_id>asdfasdf</user_id> 
    28.  <album_id>asdf</album_id> 
    29.  <album_name>asdf</album_name> 
    30.  <tags>asdfasd</tags> 
    31.  <title>asdfasdf</title> 
    32.  <content>asdfadsf</content> 
    33.  <type>asdfasdf</type> 
    34.  <copyright>asdfasdf</copyright> 
    35.  </profile>';  
    36.   
    37. $interface = 'http://localhost/test/header2.php';  
    38. $header = FormatHeader($interface,'10.1.11.1',$xml);  //修改3  
    39.   
    40. $ch = curl_init();  
    41. curl_setopt($ch, CURLOPT_URL, $interface);  
    42. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);  //设置头信息的地方  
    43. curl_setopt($ch, CURLOPT_HEADER, 0);    //不取得返回头信息  
    44. curl_setopt($ch, CURLOPT_TIMEOUT, 5);  
    45. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  
    46. $result = curl_exec($ch);  
    47.   
    48. var_dump($result);  
    49. ?>  

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

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

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

  • 相关阅读:
    java读取配置文件 xml properties的几种方法
    aix source 命令
    解决连接ORACLE错误一例:ORA01033: ORACLE 正在初始化或关闭
    oracle database character set
    spring junit 测试
    Atitit。Cas机制 软件开发 编程语言 无锁机制 java c# php
    Atitit.病毒木马的快速扩散机制原理nio 内存映射MappedByteBuffer
    Atitit.木马病毒 webftp 的原理跟个设计
    Atitit.js this错误指向window的解决方案
    Atitit.反编译apk android源码以及防止反编译apk
  • 原文地址:https://www.cnblogs.com/dengcw/p/5612955.html
Copyright © 2011-2022 走看看