Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4175434
  • 博文数量: 291
  • 博客积分: 8003
  • 博客等级: 大校
  • 技术积分: 4275
  • 用 户 组: 普通用户
  • 注册时间: 2010-10-30 18:28
文章分类

全部博文(291)

文章存档

2017年(1)

2013年(47)

2012年(115)

2011年(121)

2010年(7)

分类: 系统运维

2011-05-21 21:22:09

下面是一个模拟get或者post请求的方法支持
1.get,post方法
2.自定义参数
3.自定义header
4.返回服务器的返回内容和header
5.支持相特定的服务器请求url,适合测试cdn节点
代码如下
  1. <?php
  2. error_reporting(0);
  3. $user_agent="Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.10)";

  4. list($rheaders,$conntent)=request_url("60.28.14.148","80","get","",array("User-Agent"=> $user_agent),array());

  5. //list($rheaders,$conntent)=request_url("","","get","",array("User-Agent"=>$user_agent),array("wd"=>"test"));
  6. if($rheaders["httpstatus"]>=200&&$rheaders["httpstatus"]<=300)
  7. {

  8.   //根据返回的类型,修改header
  9.   if($rheaders['Content-Type']!="")
  10.     header('Content-Type: '.$rheaders['Content-Type']);
  11.     echo     $conntent;
  12. //    foreach($rheaders as $k => $v)echo "$k: $v
    ";

  13. }else
  14. {
  15.     //若是是302,301之类跳转的话,继续取跳转的
  16.     if($rheaders["httpstatus"]>=300&&$rheaders["httpstatus"]<=400){
  17.             list($rheaders,$conntent)=request_url("","","get",$rheaders["Location"],array("User-Agent"=> $user_agent),array());        
  18.             if($rheaders["httpstatus"]>=200&&$rheaders["httpstatus"]<=300)
  19.             {
  20.              //根据返回的类型,修改header
  21.              if($rheaders['Content-Type'])
  22.                     header('Content-Type: '.$rheaders['Content-Type']);
  23.                 echo     $conntent;
  24.             }
  25.     }
  26.     //    foreach($rheaders as $k => $v)echo "$k: $v
    ";

  27. }



  28. /*
  29.  * 模拟get,post方法向服务器请求某url的内容,返回内容和状态码
  30.  * 参数: $ip:url所在的服务器ip或者域名,当传入为空时,ip的默认值就是$aURL里包含host(ip或者域名)
  31.          $port:int,url所在的服务器的端口,当传入为空时,ip的默认值就是$aURL里包含端口,若是没有的话为80
  32.          $method:get还是post,缺省为post
  33.          $aURL:请求的url,格式为
  34.          $headers:数组,需要模拟的http头部(Referer,Content-Disposition,Expires,Cookie,Pragma,User-Agent,Accept-Language等等)
  35.                        $headers=array("Referer"=>"","User-Agent"=>"Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.10)");
  36.          $aParamsArray:数组,需要post或者get的参数
  37.                        $aParamsArray=array("id"=>123,"name"=>"abc");
  38.  * 返回值:数组:第一个元素为返回的http头部数组(http状态(默认为0,连接错误为-1)),第二个为返回的内容
  39.          
  40. */
  41. function request_url($ip,$port,$method,$aURL,$headers,$aParamsArray)
  42. {
  43.     $rheaders=array();
  44.      $rheaders["httpstatus"]=0;
  45.     //分解URL为host,端口,和路径
  46.     $URL=parse_url($aURL);
  47.     if(!$port)
  48.     {
  49.         //缺省端口为80
  50.         $port=($URL["port"]? $URL["port"]:80);    
  51.     }
  52.     if(strcasecmp($method,"get")==0){
  53.         $method="GET";
  54.     }else{
  55.         $method="POST";
  56.     }
  57.     //把post的数据放到数据段里
  58.     foreach($aParamsArray as $key=> $value)
  59.     {
  60.      if($flag!=0)
  61.      {
  62.      $params.="&";
  63.      $flag=1;
  64.      }
  65.      $params.=$key."=";
  66.      $params.=urlencode($value);
  67.      $flag=1;
  68.     }
  69.         if($method=="POST")
  70.         {
  71.      //获得数据段长度,放入Content-Length
  72.      $length=strlen($params);

  73.          }else
  74.          {
  75.           $length=0;
  76.           //把数据放到url的参数后面
  77.           $URL["query"]=$URL["query"]."&".$params;
  78.          }
  79.          if($URL["query"]){
  80.              //添加参数
  81.              $URL["path"].="?".$URL["query"].($URL["fragment"]?"#".$URL["fragment"]:"");
  82.          }
  83.      //创建socket连接
  84.      $fp=fsockopen($ip==""? $URL["host"]:$ip,$port,$errno,$errstr,10);
  85.      if(!$fp)
  86.      {
  87.           $rheaders["httpstatus"]=-1;
  88.              return array($rheaders,$errstr."--->".$errno);
  89.      }
  90.      //去掉不多余的头部
  91.      unset($headers['Host']);
  92.      unset($headers['Content-Length']);
  93.      unset($headers['Content-Type']);
  94.      //构造post请求的头
  95.      $header="$method ".$URL["path"]." HTTP/1.1\r\n";
  96.      $header.="Host:".$URL["host"]."\r\n";
  97.      foreach($headers as $k=> $v){
  98.          $header.="$k:$v\r\n";
  99.      }
  100.      if(!$header["Content-Type"]){
  101.      $header.="Content-Type:application/x-www-form-urlencoded\r\n";
  102.           }
  103.      $header.="Content-Length:".$length."\r\n";
  104.      $header.="Connection:Close\r\n\r\n";
  105.      if($method=="POST")
  106.          {
  107.          //添加post的字符串
  108.          $header.=$params."\r\n";
  109.      }
  110. //     echo $header;
  111.      //发送post的数据
  112.      fputs($fp,$header);
  113.      $inheader=1;
  114.      $lineno=0;
  115.      $conntent="";

  116.      while(!feof($fp))
  117.      {
  118.           if($inheader){
  119.          $line=fgets($fp,1024);//读取header
  120.          }else{
  121.              if($rheaders["Content-Length"]>=0){
  122.                  $line=fread($fp,$rheaders["Content-Length"]);//读取返回内容
  123.              }else{
  124.                  $line=fread($fp,1024);//读取返回内容    
  125.              }
  126.          }
  127.      $lineno++;
  128.      if($inheader){
  129.          if($lineno==1)
  130.          {
  131.                  //从第一行,获取返回的状态码
  132.                  if(preg_match("/^HTTP\/1\.[1|0] (\d{3})/i",$line,$match)){
  133.                      $rheaders["httpstatus"]=$match[1];
  134.                  }
  135.          }
  136.      //解析http头部,把所有字段
  137.          if(preg_match("/^(.*): (.*)$/i",$line,$matches)){
  138.                         $rheaders[$matches[1]]=$matches[2];
  139.          }
  140.      }
  141.      if($inheader&&($line=="\n"||$line=="\r\n")){
  142.      $inheader=0;
  143.      continue;
  144.      }
  145.      if($inheader==0)
  146.      {
  147.          //获得返回内容
  148.      $conntent.=$line;
  149.      }
  150.      }
  151.     fclose($fp);
  152.     return array($rheaders,$conntent);
  153. }
  154. ?>
阅读(12156) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~