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

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

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

HttpRequest.class.php類文件如下:

<?php /** HttpRequest class, HTTP請(qǐng)求類,支持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ù),不會(huì)處理文件數(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);        // 讀取返回?cái)?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ù),不會(huì)處理文件數(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è)置分割標(biāo)識(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>';  ?> 

完整實(shí)例代碼可以點(diǎn)擊此處本站下載。

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

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

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

主站蜘蛛池模板: 糙汉顶弄抽插HHHH | 久久免费看少妇高潮A片2012 | 亚洲乱码中文字幕久久孕妇黑人 | 国产欧美一区二区三区视频 | 欧美一区二区在线观看 | 久久亚洲精品中文字幕 | 99re在线播放 | 永久免费精品精品永久-夜色 | 99久久婷婷国产麻豆精品电影 | 午夜性色一区二区三区不卡视频 | 99久久蜜臀亚洲AV无码精品 | 久久久久久久久女黄 | 99久久999久久久综合精品涩 | 囯产免费久久久久久国产免费 | 天天狠狠弄夜夜狠狠躁·太爽了 | 欧美一区二区激情视频 | 老妇xxxxbbbb | 久久精品视频在线看99 | 青青草 久久久 | 久久精品中文字幕有码日本 | 成人影院午夜久久影院 | 国产精品一久久香蕉国产线看 | 久久综合一个色综合网 | 中文字幕在线播放视频 | 在线观看日本免费 | 亚洲第一综合天堂另类专 | 99视频精品国产免费观看 | 天美传媒在线观看免费完整版 | 小寡妇好紧进去了好大看视频 | 韩国羞羞秘密教学子开车漫书 | proburn中文破解版下载 | 欧美另类老少配hd | 午夜爱情动作片P | 成人精品综合免费视频 | 吻嘴胸全身好爽床大全 | 精品人妻伦九区久久AAA片69 | 一品道门在线观看免费视频 | 7756短视频 | 久爱在线中文在观看 | 成人亚洲乱码在线 | 亚洲欧美日本中文子不卡 |