title: PHP Manual GZip Encoding
body: <p>I was testing my website with Page Speed and the result was around 70/100. Enable Compression was the first and most important factor in slowing it down.</p>
  
  <p>I know that I can do that by modifying the php.ini to automatically do that but I was more interested in the manual method (<code>gzencode</code>). </p>
  
  <p>The problem is either all browsers fail in opening the website (Firefox: "The page you are trying to view cannot be shown because it uses an invalid or unsupported form of compression.", Chrome: "303, ERR Content Encoding", etc.) or they display the encoded string.</p>
  
  <p>Live Headers shows that the browser accepts the encoding, and the response has the content type set, but it still fails.</p>
  
  <pre><code>GET / HTTP/1.1
  Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
  Accept-Encoding: gzip,deflate
  
  HTTP/1.1 200 OK
  Content-Encoding: gzip
  Content-Length: 5827
  Vary: Accept-Encoding
  </code></pre>
  
  <hr>
  
  <pre><code>private function _compress($data) {
      //return trim(preg_replace(array('/\&gt;[^\S ]+/s','/[^\S ]+\&lt;/s','/(\s)+/s'), array('&gt;','&lt;','\\1'), $data));
      $supportsGzip = strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== false;
  
      ob_start();
      if ($supportsGzip) {
          echo gzencode(trim(preg_replace('/\s+/', ' ', $data)), 9);
      } else {
          echo $data;
      }
  
      $content = ob_get_contents();
      header("content-type: text/html; charset: UTF-8");
      header("cache-control: must-revalidate");
      $offset = 60 * 60;
      $expire = "expires: " . gmdate("D, d M Y H:i:s", time() + $offset) . " GMT";
      header($expire);
      header('Content-Length: ' . strlen($content));
      header('Vary: Accept-Encoding');
      ob_end_clean();
      echo $content;
  }
  </code></pre>
  
  <p>If I change the Content-Encoding to zlib, I get the encoded string:</p>
  
  <pre><code>‹������ÕZÿsÛ¶ÿW^‘¥²o‘¨/–-Ë–ÚØ‰_Ôµ•õÚ_v I°I‚!A©j–Öºnçÿb·»%ÍÚë²nëå?‘þ›=€¤L)’,ÛIw&gt;ŸEâxïáƒ÷°ùÞ½O¶Ÿïß‡ŽtlØ¼µ·» $kŸ•¶ ã^ã&lt;Üƒ•\¾� Ÿº—\¸Ô6ŒûŽ”^Õ0z½^®WÊ ¿m4ÅjÅ°…XÎ’©Ã¦ænS·]#ÌÕF-|8LRPL²ìIÈ»5²-\É\™mô=FÀŒJ5"Ù—RóÝ�³Cý€ÉZ([ÙŠb%¹´YýÑãáîcx}±iD´˜¿KV#4”á§x&gt;¬°à®íÒ ãpÅËæî1øÌ®‘@öm
  </code></pre>
  
  <p>I don’t really care anymore about getting the compression as much as I want to know why its not working.</p>
  
  <p>Cheers,</p>