天天躁日日躁狠狠躁AV麻豆-天天躁人人躁人人躁狂躁-天天澡夜夜澡人人澡-天天影视香色欲综合网-国产成人女人在线视频观看-国产成人女人视频在线观看

PHP實現(xiàn)支持GET,POST,Multipart/form-data的HTTP請求類

本文實例講述了php實現(xiàn)支持GET,POST,Multipart/form-data的HTTP請求類及其應(yīng)用,分享給大家供大家參考。具體如下:

HttpRequest.class.php類文件如下:

<?php /** HttpRequest class, HTTP請求類,支持GET,POST,Multipart/form-data *  Date:  2013-09-25 *  Author: fdipzone *  Ver:  1.0 * *  Func: *  public setConfig   設(shè)置連接參數(shù) *  public setFormdata  設(shè)置表單數(shù)據(jù) *  public setFiledata  設(shè)置文件數(shù)據(jù) *  public send     發(fā)送數(shù)據(jù) *  private connect    創(chuàng)建連接 *  private disconnect  斷開連接 *  private sendGet    get 方式,處理發(fā)送的數(shù)據(jù),不會處理文件數(shù)據(jù) *  private sendPost   post 方式,處理發(fā)送的數(shù)據(jù) *  private sendMultipart multipart 方式,處理發(fā)送的數(shù)據(jù),發(fā)送文件推薦使用此方式 */  class HttpRequest{ // class start    private $_ip = '';   private $_host = '';   private $_url = '';   private $_port = '';   private $_errno = '';   private $_errstr = '';   private $_timeout = 15;   private $_fp = null;      private $_formdata = array();   private $_filedata = array();     // 設(shè)置連接參數(shù)   public function setConfig($config){     $this->_ip = isset($config['ip'])? $config['ip'] : '';     $this->_host = isset($config['host'])? $config['host'] : '';     $this->_url = isset($config['url'])? $config['url'] : '';     $this->_port = isset($config['port'])? $config['port'] : '';     $this->_errno = isset($config['errno'])? $config['errno'] : '';     $this->_errstr = isset($config['errstr'])? $config['errstr'] : '';     $this->_timeout = isset($confg['timeout'])? $confg['timeout'] : 15;      // 如沒有設(shè)置ip,則用host代替     if($this->_ip==''){       $this->_ip = $this->_host;     }   }    // 設(shè)置表單數(shù)據(jù)   public function setFormData($formdata=array()){     $this->_formdata = $formdata;   }    // 設(shè)置文件數(shù)據(jù)   public function setFileData($filedata=array()){     $this->_filedata = $filedata;   }    // 發(fā)送數(shù)據(jù)   public function send($type='get'){      $type = strtolower($type);      // 檢查發(fā)送類型     if(!in_array($type, array('get','post','multipart'))){       return false;     }      // 檢查連接     if($this->connect()){        switch($type){         case 'get':           $out = $this->sendGet();           break;          case 'post':           $out = $this->sendPost();           break;          case 'multipart':           $out = $this->sendMultipart();           break;       }        // 空數(shù)據(jù)       if(!$out){         return false;       }        // 發(fā)送數(shù)據(jù)       fputs($this->_fp, $out);        // 讀取返回數(shù)據(jù)       $response = '';        while($row = fread($this->_fp, 4096)){         $response .= $row;       }        // 斷開連接       $this->disconnect();        $pos = strpos($response, "/r/n/r/n");       $response = substr($response, $pos+4);        return $response;      }else{       return false;     }   }    // 創(chuàng)建連接   private function connect(){     $this->_fp = fsockopen($this->_ip, $this->_port, $this->_errno, $this->_errstr, $this->_timeout);     if(!$this->_fp){       return false;     }     return true;   }    // 斷開連接   private function disconnect(){     if($this->_fp!=null){       fclose($this->_fp);       $this->_fp = null;     }   }    // get 方式,處理發(fā)送的數(shù)據(jù),不會處理文件數(shù)據(jù)   private function sendGet(){      // 檢查是否空數(shù)據(jù)     if(!$this->_formdata){       return false;     }      // 處理url     $url = $this->_url.'?'.http_build_query($this->_formdata);          $out = "GET ".$url." http/1.1/r/n";     $out .= "host: ".$this->_host."/r/n";     $out .= "connection: close/r/n/r/n";      return $out;   }    // post 方式,處理發(fā)送的數(shù)據(jù)   private function sendPost(){      // 檢查是否空數(shù)據(jù)     if(!$this->_formdata && !$this->_filedata){       return false;     }      // form data     $data = $this->_formdata? $this->_formdata : array();      // file data     if($this->_filedata){       foreach($this->_filedata as $filedata){         if(file_exists($filedata['path'])){           $data[$filedata['name']] = file_get_contents($filedata['path']);         }       }     }      if(!$data){       return false;     }      $data = http_build_query($data);      $out = "POST ".$this->_url." http/1.1/r/n";     $out .= "host: ".$this->_host."/r/n";     $out .= "content-type: application/x-www-form-urlencoded/r/n";     $out .= "content-length: ".strlen($data)."/r/n";     $out .= "connection: close/r/n/r/n";     $out .= $data;      return $out;   }    // multipart 方式,處理發(fā)送的數(shù)據(jù),發(fā)送文件推薦使用此方式   private function sendMultipart(){      // 檢查是否空數(shù)據(jù)     if(!$this->_formdata && !$this->_filedata){       return false;     }      // 設(shè)置分割標識     srand((double)microtime()*1000000);     $boundary = '---------------------------'.substr(md5(rand(0,32000)),0,10);      $data = '--'.$boundary."/r/n";      // form data     $formdata = '';      foreach($this->_formdata as $key=>$val){       $formdata .= "content-disposition: form-data; name=/"".$key."/"/r/n";       $formdata .= "content-type: text/plain/r/n/r/n";       if(is_array($val)){         $formdata .= json_encode($val)."/r/n"; // 數(shù)組使用json encode后方便處理       }else{         $formdata .= rawurlencode($val)."/r/n";       }       $formdata .= '--'.$boundary."/r/n";     }      // file data     $filedata = '';      foreach($this->_filedata as $val){       if(file_exists($val['path'])){         $filedata .= "content-disposition: form-data; name=/"".$val['name']."/"; filename=/"".$val['filename']."/"/r/n";         $filedata .= "content-type: ".mime_content_type($val['path'])."/r/n/r/n";         $filedata .= implode('', file($val['path']))."/r/n";         $filedata .= '--'.$boundary."/r/n";       }     }      if(!$formdata && !$filedata){       return false;     }      $data .= $formdata.$filedata."--/r/n/r/n";      $out = "POST ".$this->_url." http/1.1/r/n";     $out .= "host: ".$this->_host."/r/n";     $out .= "content-type: multipart/form-data; boundary=".$boundary."/r/n";     $out .= "content-length: ".strlen($data)."/r/n";     $out .= "connection: close/r/n/r/n";     $out .= $data;      return $out;   } } // class end  ?>

demo示例程序如下:

<?php require('HttpRequest.class.php');  $config = array(       'ip' => 'demo.fdipzone.com', // 如空則用host代替       'host' => 'demo.fdipzone.com',       'port' => 80,       'errno' => '',       'errstr' => '',       'timeout' => 30,       'url' => '/getapi.php',       //'url' => '/postapi.php',       //'url' => '/multipart.php' );  $formdata = array(   'name' => 'fdipzone',   'gender' => 'man' );  $filedata = array(   array(     'name' => 'photo',     'filename' => 'photo.jpg',     'path' => 'photo.jpg'   ) );  $obj = new HttpRequest(); $obj->setConfig($config); $obj->setFormData($formdata); $obj->setFileData($filedata); $result = $obj->send('get'); //$result = $obj->send('post'); //$result = $obj->send('multipart');  echo '<pre>'; print_r($result); echo '</pre>';  ?> 

完整實例代碼可以點擊此處本站下載。

希望本文所述對大家的php程序設(shè)計有所幫助。

php技術(shù)PHP實現(xiàn)支持GET,POST,Multipart/form-data的HTTP請求類,轉(zhuǎn)載需保留來源!

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯(lián)系我們修改或刪除,多謝。

主站蜘蛛池模板: 高H高肉强J短篇NP | 青青草原国产 | 日韩成人性视频 | 亚洲无人区码二码三码区别图 | 夜色爽爽爽久久精品日韩 | 性色香蕉AV久久久天天网 | 97超视频在线观看 | 任你躁国语自产二区在线播放 | 女性BBWBBWBBWBBW | 爽爽影院免费观看 | 特级做A爰片毛片免费69 | 亚洲精品第一页 | 神马影院午夜理论二 | 午夜国产羞羞视频免费网站 | 国产精品久久久久久久久99热 | 伊人久久综合影院首页 | 婷婷激情综合色五月久久竹菊影视 | 美女穿丝袜被狂躁动态图 | 午夜看片福利在线观看 | 大肥婆丰满大肥奶bbw肥 | 欧美人与动牲交A精品 | 韩国和日本免费不卡在线 | 毛片999| 性xxxx直播放免费 | 国产精品无码亚洲网 | 云南14学生真实初次破初视频 | 国产区免费在线观看 | 日韩做A爰片久久毛片A片毛茸茸 | 晓雪老师我要进你里面好爽 | 高h肉文np | 麻豆免费高清完整版 | 久久亚洲精品AV成人无 | 欧美日韩无套内射另类 | 花蝴蝶高清影视视频在线播放 | 爽娇妻快高h | 国产高清视频在线播放www色 | 国产亚洲精品第一区香蕉 | 亚洲成 人a影院青久在线观看 | 手机在线看片欧美亚洲 | 一边捏奶头一边啪高潮会怎么样 | 欧美另类jizzhd |