HTTP_Encoder::encode PHP Method

encode() public method

If the encode method is '' (none) or compression level is 0, or the 'zlib' extension isn't loaded, we return false. Then the appropriate gz_* function is called to compress the content. If this fails, false is returned. The header "Vary: Accept-Encoding" is added. If encoding is successful, the Content-Length header is updated, and Content-Encoding is also added.
public encode ( integer $compressionLevel = null ) : boolean
$compressionLevel integer given to zlib functions. If not given, the class default will be used.
return boolean success true if the content was actually compressed
    public function encode($compressionLevel = null)
    {
        if (!self::isBuggyIe()) {
            $this->_headers['Vary'] = 'Accept-Encoding';
        }
        if (null === $compressionLevel) {
            $compressionLevel = self::$compressionLevel;
        }
        if ('' === $this->_encodeMethod[0] || $compressionLevel == 0 || !extension_loaded('zlib')) {
            return false;
        }
        if ($this->_encodeMethod[0] === 'deflate') {
            $encoded = gzdeflate($this->_content, $compressionLevel);
        } elseif ($this->_encodeMethod[0] === 'gzip') {
            $encoded = gzencode($this->_content, $compressionLevel);
        } else {
            $encoded = gzcompress($this->_content, $compressionLevel);
        }
        if (false === $encoded) {
            return false;
        }
        $this->_headers['Content-Length'] = $this->_useMbStrlen ? (string) mb_strlen($encoded, '8bit') : (string) strlen($encoded);
        $this->_headers['Content-Encoding'] = $this->_encodeMethod[1];
        $this->_content = $encoded;
        return true;
    }

Usage Example

Ejemplo n.º 1
0
 /**
  * Gzip (encode) the HTTP response and write to output.
  * @param string $strBody        The content that should be in the response.
  * @param string $strContentType The MIME type of the content.
  */
 public static function send($strBody, $strContentType = "text/html")
 {
     $objEncoder = new \HTTP_Encoder(array("content" => $strBody, "type" => $strContentType));
     $objEncoder->encode();
     $objEncoder->sendAll();
     exit;
 }
All Usage Examples Of HTTP_Encoder::encode