php 里面摸拟post请求的代码

来源:互联网 发布:mysql 视图查询效率 编辑:程序博客网 时间:2024/06/16 15:50
  改造公司的网站,将原来的java桥的接口改成http形势的,以减轻java桥的压力,局域网内这种方法还是可取的,速度还很快,这是我用到的php摸拟post的方法,网上很多,但我觉得我这个还是很好用的,跟大家分享一下。

   
  

  
    publicstatic function ServiceCall( $post = '', $ip = '', $timeout = 15,$block = TRUE)
    {

      $return = '';
       //$url ='http://192.168.60.44:9800/flyingCityWAP/externalAction!external.action';;
      $url ='http://192.168.60.17:9008/flyingCityWAP/externalAction!external.action';
      
      $matches = parse_url($url);
      !isset($matches['host'])&& $matches['host'] = '';
      !isset($matches['path'])&& $matches['path'] = '';
      !isset($matches['query'])&& $matches['query'] = '';
      !isset($matches['port'])&& $matches['port'] = '';
      $host = $matches['host'];
      $path = $matches['path'] ?$matches['path'].($matches['query'] ? '?'.$matches['query'] : '') :'/';
      $port = !empty($matches['port']) ?$matches['port'] : 80;
      
       if($post) {
         $out = "POST $path HTTP/1.0\r\n";
         $out .= "Accept: **\r\n";
         $out .= "Accept-Language: zh-cn\r\n";
         $out .= "User-Agent:$_SERVER[HTTP_USER_AGENT]\r\n";
         $out .= "Host: $host\r\n";
         $out .= "Connection: Close\r\n";
         $out .= "Cookie: $cookie\r\n\r\n";
      }
       
      $fp = fsockopen(($ip ? $ip : $host), $port,$errno, $errstr, 1);
      if(!$fp) {
         return '';
      } else {
         stream_set_blocking($fp, $block);
         stream_set_timeout($fp, $timeout);
         @fwrite($fp, $out);
         $status = stream_get_meta_data($fp);
        
         if(!$status['timed_out']) {
            while (!feof($fp)) {
               if(($header = @fgets($fp))&& ($header == "\r\n"||  $header == "\n")) {
                  break;
               }
            }
    
            $stop = false;
            while(!feof($fp)&& !$stop) {
               $data = fread($fp, ($limit == 0 || $limit> 118192 ? 118192 : $limit));
               $return .= $data;
               if($limit) {
                  $limit -= strlen($data);
                  $stop = $limit <= 0;
               }
            }
         }
         @fclose($fp);
         return $return;
      }

    }
原创粉丝点击