PHP implements HTTP request classes that support GET POST Multipart and form data

  • 2021-07-21 07:49:43
  • OfStack

This paper describes the HTTP request class and its application supporting GET, POST, Multipart/form-data by PHP, and shares it for your reference. The details are as follows:

The HttpRequest. class. php class files are as follows:


<?php 
/** HttpRequest class, HTTP Request class, support GET,POST,Multipart/form-data 
*  Date:  2013-09-25 
*  Author: fdipzone 
*  Ver:  1.0 
* 
*  Func: 
*  public setConfig    Setting connection parameters  
*  public setFormdata   Set up form data  
*  public setFiledata   Set file data  
*  public send      Send data  
*  private connect     Create a connection  
*  private disconnect   Disconnect  
*  private sendGet    get  Mode , Processing sent data , File data will not be processed  
*  private sendPost   post  Mode , Processing sent data  
*  private sendMultipart multipart  Mode , Processing sent data , This method is recommended for sending files  
*/ 
 
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(); 
 
 
  //  Setting connection parameters  
  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; 
 
    //  If not set ip , then use host Substitute  
    if($this->_ip==''){ 
      $this->_ip = $this->_host; 
    } 
  } 
 
  //  Set up form data  
  public function setFormData($formdata=array()){ 
    $this->_formdata = $formdata; 
  } 
 
  //  Set file data  
  public function setFileData($filedata=array()){ 
    $this->_filedata = $filedata; 
  } 
 
  //  Send data  
  public function send($type='get'){ 
 
    $type = strtolower($type); 
 
    //  Check sending type  
    if(!in_array($type, array('get','post','multipart'))){ 
      return false; 
    } 
 
    //  Check connection  
    if($this->connect()){ 
 
      switch($type){ 
        case 'get': 
          $out = $this->sendGet(); 
          break; 
 
        case 'post': 
          $out = $this->sendPost(); 
          break; 
 
        case 'multipart': 
          $out = $this->sendMultipart(); 
          break; 
      } 
 
      //  Null data  
      if(!$out){ 
        return false; 
      } 
 
      //  Send data  
      fputs($this->_fp, $out); 
 
      //  Read the returned data  
      $response = ''; 
 
      while($row = fread($this->_fp, 4096)){ 
        $response .= $row; 
      } 
 
      //  Disconnect  
      $this->disconnect(); 
 
      $pos = strpos($response, "\r\n\r\n"); 
      $response = substr($response, $pos+4); 
 
      return $response; 
 
    }else{ 
      return false; 
    } 
  } 
 
  //  Create a connection  
  private function connect(){ 
    $this->_fp = fsockopen($this->_ip, $this->_port, $this->_errno, $this->_errstr, $this->_timeout); 
    if(!$this->_fp){ 
      return false; 
    } 
    return true; 
  } 
 
  //  Disconnect  
  private function disconnect(){ 
    if($this->_fp!=null){ 
      fclose($this->_fp); 
      $this->_fp = null; 
    } 
  } 
 
  // get  Mode , Processing sent data , File data will not be processed  
  private function sendGet(){ 
 
    //  Check whether the data is empty  
    if(!$this->_formdata){ 
      return false; 
    } 
 
    //  Deal with 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  Mode , Processing sent data  
  private function sendPost(){ 
 
    //  Check whether the data is empty  
    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  Mode , Processing sent data , This method is recommended for sending files  
  private function sendMultipart(){ 
 
    //  Check whether the data is empty  
    if(!$this->_formdata && !$this->_filedata){ 
      return false; 
    } 
 
    //  Setting Segmentation Identifier  
    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"; //  Array usage json encode Post-convenient treatment  
      }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 
 
?>

The demo sample program is as follows:


<?php 
require('HttpRequest.class.php'); 
 
$config = array( 
      'ip' => 'demo.fdipzone.com', //  Use if empty host Substitute  
      '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>'; 
 
?> 

The complete example code can be downloaded by clicking here on this site.

I hope this article is helpful to everyone's PHP programming.


Related articles: