溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

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

發(fā)布時(shí)間:2021-06-25 15:07:38 來(lái)源:億速云 閱讀:287 作者:小新 欄目:開發(fā)技術(shù)

這篇文章給大家分享的是有關(guān)PHP如何實(shí)現(xiàn)支持GET,POST,Multipart/form-data的HTTP請(qǐng)求類的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過(guò)來(lái)看看吧。

具體如下:

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>'; 
 
?>

感謝各位的閱讀!關(guān)于“PHP如何實(shí)現(xiàn)支持GET,POST,Multipart/form-data的HTTP請(qǐng)求類”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI