整理 fsockopen 请求实现方式

来源:互联网 发布:vivo的软件商店 编辑:程序博客网 时间:2024/05/18 05:24

今天有空整理使用 fsockopen 的请求实现方式

. 模似用户请求

. 文件上传带 from 参数

. 发起http异步请求


<?php




namespace AppHttp\com;


class CurlHttp {




const METHOD_GET = 'GET';
const METHOD_POST = 'POST';




/**
*  模似用户请求
* @param String $req_url
* @param String $method
* @param Array $param
* @param String $ip
* @param int $connect_time
* @return String
* @throws \Exception
*/
public static function fsockopenAnalogUserRequest($req_url,$method='get',$param=array(),$ip='',$connect_time=5) {


$p = parse_url($req_url); 
$host = $p['host'];
$path = isset($p['path'])?$p['path']:'/';
$ip ==  '' && $ip = $host;
$port = isset($p['port'])?$p['port']:80;


$method = strtoupper($method);
if(!in_array($method,array('GET','POST'))) {
throw new \Exception('Method Error , Method in Get or Post');
}


$param_str = '';
if(!empty($param)) {
$param_str = http_build_query($param);
$method == 'GET' && $path .= '?' . $param_str;
}



$fh = fsockopen($ip,$port, $errno, $errstr,$connect_time);
if($fh == false) {
throw new \Exception('Connect fail error code '.$error.' error info '.$errstr);
}else {
$eof = "\r\n";
$http = "{$method} {$path} HTTP/1.1{$eof}";
$http .= "HOST:{$ip}{$eof}";
$http .= "User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36{$eof}";
$http .= "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8{$eof}";
$http .= "Accept-Language:zh-CN,zh;q=0.8{$eof}";
//如果不加下面这一句,请求会阻塞很久
$http .= "Connection:close{$eof}";//告诉服务器响应完就断开。


if($method == 'POST') {
$param_length = strlen($param_str);
$http .= "Content-Type: application/x-www-form-urlencoded{$eof}";
$http .= "Content-Length: {$param_length}{$eof}";
}
$http .= "{$eof}";
$http .= $param_str;

$res = '';
fwrite($fh,$http);
while(!feof($fh)) {
$res .= fgets($fh,4096);
}
fclose($fh); 
return $res;
}


}




/**
*  文件上传带 from 参数
* @param $form_data array('key'=>val,'key'=>val)
* @param $file_data array('form_file_key'=>file_url,'form_file_key1'=>file_url1)
*
*/
public static function fsockopenPostUpload($form_data,$file_data,$req_url,$ip='',$connect_time=5) {




$p = parse_url($req_url); 
$host = $p['host'];
$path = isset($p['path'])?$p['path']:'/';
isset($p['query']) && $path .= '?'.$p['query'];
$ip ==  '' && $ip = $host;
$port = isset($p['port'])?$p['port']:80;




$boundary = "---------------------------".substr(md5(rand(0,32000)),0,10);  
       $data = "--$boundary\r\n";  
       
       /*
       $form_data = array(  
           'name' => 'fdipzone',  
           'gender' => 'man',  
       ); */ 
       foreach($form_data as $k=>$v) {
           $data .= "Content-Disposition: form-data; name=\"{$k}\"\r\n";  
           $data .= "Content-type:text/plain\r\n\r\n";  
           $data .= rawurlencode($v)."\r\n";  
           $data .= "--$boundary\r\n";  
       }
       /*
       $file_data = array(  
           $real_path
       );*/  
       foreach($file_data as $k=>$file) {
           
           $_data = pathinfo($file);
           $finfo = finfo_open(FILEINFO_MIME_TYPE); // 返回 mime 类型
           $content_type = finfo_file($finfo, $file);
           finfo_close($finfo);
           $data .= "Content-Disposition: form-data; name=\"{$k}\"; filename=\"".$_data['basename']."\"\r\n";  
           $data .= "Content-Type: ".$content_type."\r\n\r\n";  
           $data .= implode("",file($file))."\r\n";  
           $data .= "--$boundary\r\n";  
           
       }
       
       $out = "POST {$path} HTTP/1.1\r\n";  
       $out .= "Host:{$host}\r\n";  
       $out .= "Content-type:multipart/form-data; boundary=$boundary\r\n"; // multipart/form-data  
       $out .= "Content-length:".strlen($data)."\r\n";  
       $out .= "Connection:close\r\n\r\n";  
       $out .= "{$data}";  
       
       
       $fh = fsockopen($ip ,$port, $errno, $errstr,$connect_time);
       $res = '';
       fwrite($fh,$out);
       while(!feof($fh)) {
               $res .= fgets($fh,4096);
       }
       fclose($fh); 


       return $res;
       $pos = strpos($res, "\r\n\r\n");  
       $res = substr($res, $pos+4);  
       
       var_dump($res);


}




/**
    * 发起http异步请求
    * @param string $url http地址
    * @param string $method 请求方式
    * @param array $params 参数
    * @param string $ip 支持host配置
    * @param int $connectTimeout 连接超时,单位为秒
    * @throws Exception
    */
   public static function exec($url, $method = self::METHOD_GET, $params = array(), $ip = null, $connectTimeout = 5)
   {


       $urlInfo = parse_url($url);


       $host = $urlInfo['host'];
       $port = isset($urlInfo['port']) ? $urlInfo['port'] : 80;
       $path = isset($urlInfo['path']) ? $urlInfo['path'] : '/';


       !$ip && $ip = $host;


       $method = strtoupper(trim($method)) !== self::METHOD_POST ? self::METHOD_GET : self::METHOD_POST; 
       $params = http_build_query($params);


       if($method === self::METHOD_GET && strlen($params) > 0){
           $path .= '?' . $params;
       }


       $fp = fsockopen($ip, $port, $errorCode, $errorInfo, $connectTimeout);
       if($fp === false){
           throw new Exception('Connect failed , error code: ' . $errorCode . ', error info: ' . $errorInfo);
       }else{
               
           $http  = "$method $path HTTP/1.1\r\n";
           $http .= "Host: $host\r\n";
           $http .= "Content-type: application/x-www-form-urlencoded\r\n";
           $method === self::METHOD_POST && $http .= "Content-Length: " . strlen($params) . "\r\n";
           $http .= "\r\n";
           $method === self::METHOD_POST && $http .= $params . "\r\n\r\n";
           if(fwrite($fp, $http) === false || fclose($fp) === false){
               throw new Exception('Request failed.');
           }
       }
   }






}






?>