天天躁日日躁狠狠躁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)系我們修改或刪除,多謝。

主站蜘蛛池模板: 日本撒尿特写 | 黄色软件视频app | 伊人久久久久久久久香港 | 亚洲国产日韩欧美高清片a 亚洲国产日韩a精品乱码 | 久久亚洲国产精品亚洲 | YELLOW日本动漫免费动漫 | 久久91精品国产91久 | 亚洲AV久久无码精品国产网站 | 阿娇和冠希13分钟在线观看 | 日韩亚洲国产欧美免费观看 | 欧美高跟镣铐bdsm视频 | 一本道久久综合久久88 | 无套内射CHINESEHD | 伊人久久大香线蕉综合电影 | 中文字幕无码亚洲字幕成A人蜜桃 | 亚洲性无码av在线 | 国产在线一区二区三区四区 | 99视频在线免费 | 青青草原在线免费 | 国产产一区二区三区久久毛片国语 | 色欲人妻无码AV精品一区二区 | 毛片999 | 伊人久久亚洲精品一区 | 好大好硬好爽好深好硬视频 | 99re久久精品在线播放 | 99成人在线| 国产亚洲精品久久久久久一区二区 | 无码区国产区在线播放 | FREE17一18外女破 | 久久毛片网站 | 成电影人免费网站 | 一个人HD在线观看免费高清视频 | 成人毛片手机版免费看 | 一个人免费观看完整视频日本 | 老师真棒无遮瑕版漫画免费 | 飘雪韩国在线观看免费高清完整版 | 日本无码毛片久久久九色综合 | 久久亚洲视频 | 久久久无码精品一区二区三区 | 秋霞在线观看视频一区二区三区 | 黄色三级视频在线 |