PHP Classes

File: classes/http/request/http-request-abstract.php

Recommend this page to a friend!
  Classes of Gonzalo Chumillas   HttpRequest   classes/http/request/http-request-abstract.php   Download  
File: classes/http/request/http-request-abstract.php
Role: Class source
Content type: text/plain
Description: Class source
Class: HttpRequest
Send HTTP requests to remote servers
Author: By
Last change: docfix
Date: 9 years ago
Size: 4,455 bytes
 

Contents

Class file image Download
<?php
/**
 * This file is part of Soloproyectos common library.
 *
 * @author Gonzalo Chumillas <gchumillas@email.com>
 * @license https://github.com/soloproyectos/php.common-libs/blob/master/LICENSE BSD 2-Clause License
 * @link https://github.com/soloproyectos/php.common-libs
 */
namespace com\soloproyectos\common\http\request;
use
com\soloproyectos\common\arr\ArrHelper;
use
com\soloproyectos\common\http\request\HttpRequestFormData;
use
com\soloproyectos\common\text\TextHelper;

/**
 * Class HttpRequestAbstract.
 *
 * @package Http
 * @author Gonzalo Chumillas <gchumillas@email.com>
 * @license https://github.com/soloproyectos/php.common-libs/blob/master/LICENSE BSD 2-Clause License
 * @link https://github.com/soloproyectos/php.common-libs
 */
abstract class HttpRequestAbstract
{
   
/**
     * Configuration.
     * @var HttpRequestConfig
     */
   
protected $config = null;

   
/**
     * Associative array of parameters.
     * @var array of strings
     */
   
protected $params = array();

   
/**
     * Associative array of form parameters.
     * @var array of strings
     */
   
protected $formParams = array();

   
/**
     * Form boundary.
     * @var string
     */
   
private $_formBoundary = "";

   
/**
     * Constructor.
     *
     * @param HttpRequestConfig $config Configuration
     */
   
public function __construct($config = null)
    {
       
$this->_formBoundary = "------" . uniqid("FormBoundary");
       
$this->config = $config !== null? $config: new HttpRequestConfig();
    }

   
/**
     * Gets a parameter.
     *
     * This function returns `null` if the parameter does not exist.
     *
     * @param string $name Parameter name
     *
     * @return string
     */
   
public function getParam($name)
    {
        return
ArrHelper::get($this->params, $name);
    }

   
/**
     * Sets a parameter value
     *
     * @param string $name Parameter name
     * @param string $value Value
     *
     * @return void
     */
   
public function setParam($name, $value)
    {
       
ArrHelper::set($this->params, $name, $value);
    }

   
/**
     * Gets a 'form parameter'.
     *
     * This function returns `null` if the parameter does not exist.
     *
     * @param string $name Parameter name
     *
     * @return HttpRequestFormData
     */
   
public function getFormParam($name)
    {
        return
ArrHelper::get($this->formParams, $name);
    }

   
/**
     * Sets a 'form parameter'.
     *
     * @param string $name Parameter name
     * @param string|HttpRequestFormData $value Value
     *
     * @return void
     */
   
public function setFormParam($name, $value)
    {
        if (
is_string($value)) {
           
$value = new HttpRequestFormData($value);
        }

       
$this->config->setContentTypeOption("boundary", $this->_formBoundary);
       
ArrHelper::set($this->formParams, $name, $value);
    }

   
/**
     * Gets 'form data'.
     *
     * This function is used to set the 'content' option. For example:
     * ```php
     * $config = new HttpRequestConfig();
     * $config->setOption('content', $this->getFormData());
     * ```
     *
     * @return string
     */
   
public function getFormData()
    {
       
$ret = "";

        if (
count($this->formParams) > 0) {
            foreach (
$this->formParams as $name => $param) {
               
$str = "--" . $this->_formBoundary;

               
$contentDisposition = "Content-Disposition: form-data; name=" . urlencode($name);
               
$filename = $param->getFilename();
                if (!
TextHelper::isEmpty($filename)) {
                   
$contentDisposition .= "; filename=" . urlencode($filename);
                }

               
$contentType = "";
               
$mimeType = $param->getMimeType();
                if (!
TextHelper::isEmpty($mimeType)) {
                   
$contentType = "Content-Type: $mimeType";
                }

               
$content = "\n" . $param->getData();

               
$str = TextHelper::concat("\n", $str, $contentDisposition, $contentType, $content);
               
$ret = TextHelper::concat("\n", $ret, $str);
            }
           
$ret = TextHelper::concat("\n", $ret, "--" . $this->_formBoundary . "--\r\n");
        }

        return
$ret;
    }

   
/**
     * Sends a HTTP request.
     *
     * @param string $url URL
     *
     * @return string
     */
   
abstract public function send($url);
}