Remote Request Headers (GZIP/DEFLATE)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Peter Kelly
Forum Contributor
Posts: 143
Joined: Fri Jan 14, 2011 5:33 pm
Location: England
Contact:

Remote Request Headers (GZIP/DEFLATE)

Post by Peter Kelly »

So I have had a look around on google and I can find sites to test the ENCODING of a website e.g if it has gzip or deflate enabled but how do you actually get these headers? Because this is included in the request and not the response :/
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

Re: Remote Request Headers (GZIP/DEFLATE)

Post by twinedev »

Check these out to see if they will work for ya:

http://us3.php.net/manual/en/function.headers-list.php
http://us3.php.net/manual/en/function.a ... eaders.php

If you are wanting to just see what your page gives when you browse it, the easiest way would be to use WebDeveloper Toolbar for Firefox (http://chrispederick.com/work/web-developer/), a must have for web developers, only second to Firebug. In the menu, Choose "View Response Headers" at the bottom of the Information menu.

Another great way (on a windows machine), is to run Fiddler, which by normal installs tracks all http requests and can show you all kinds of information, including header information. (This is another great tool for developers, helps a TON for tracing AJAX calls as you can directly see the raw info sent and received from the server, as well as let you see how long it takes all components to load on the site)

-Greg
Peter Kelly
Forum Contributor
Posts: 143
Joined: Fri Jan 14, 2011 5:33 pm
Location: England
Contact:

Re: Remote Request Headers (GZIP/DEFLATE)

Post by Peter Kelly »

twinedev wrote:Check these out to see if they will work for ya:

http://us3.php.net/manual/en/function.headers-list.php
http://us3.php.net/manual/en/function.a ... eaders.php

If you are wanting to just see what your page gives when you browse it, the easiest way would be to use WebDeveloper Toolbar for Firefox (http://chrispederick.com/work/web-developer/), a must have for web developers, only second to Firebug. In the menu, Choose "View Response Headers" at the bottom of the Information menu.

Another great way (on a windows machine), is to run Fiddler, which by normal installs tracks all http requests and can show you all kinds of information, including header information. (This is another great tool for developers, helps a TON for tracing AJAX calls as you can directly see the raw info sent and received from the server, as well as let you see how long it takes all components to load on the site)

-Greg
I have looked at them in principle they would be ideal but they cant check a remote website.
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: Remote Request Headers (GZIP/DEFLATE)

Post by Jade »

Couldn't you just check with get_headers()?
Peter Kelly
Forum Contributor
Posts: 143
Joined: Fri Jan 14, 2011 5:33 pm
Location: England
Contact:

Re: Remote Request Headers (GZIP/DEFLATE)

Post by Peter Kelly »

Get_Headers is the response not the request so doesn't include the ENCODING. Basically all I want is to be able to detect if the page has been compressed using gzip or deflate etc.
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Re: Remote Request Headers (GZIP/DEFLATE)

Post by Jade »

Gah sorry I'm out of PHP based ideas then. Perhaps you could use something like WireShark and then save the results to a database. Not the greatest solution but at least you'd get the header data.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Re: Remote Request Headers (GZIP/DEFLATE)

Post by superdezign »

If this is a one-time test, use this: http://www.whatsmyip.org/http_compression/

In order to check if the server is capable of serving up a GZIPped response in PHP, request one. You can do this by setting cURL's CURLOPT_ENCODING option to 'gzip'. This is the equivalent of a browser telling the server that it is capable of handling GZIPped responses. Now, technically this doesn't mean that a capable server has to serve you GZIPped content, but Apache will by default AFAIK.

You can check DEFLATE the same way.
User avatar
flying_circus
Forum Regular
Posts: 732
Joined: Wed Mar 05, 2008 10:23 pm
Location: Sunriver, OR

Re: Remote Request Headers (GZIP/DEFLATE)

Post by flying_circus »

superdezign wrote:If this is a one-time test, use this: http://www.whatsmyip.org/http_compression/

In order to check if the server is capable of serving up a GZIPped response in PHP, request one. You can do this by setting cURL's CURLOPT_ENCODING option to 'gzip'. This is the equivalent of a browser telling the server that it is capable of handling GZIPped responses. Now, technically this doesn't mean that a capable server has to serve you GZIPped content, but Apache will by default AFAIK.

You can check DEFLATE the same way.
I was just about to post a code sample for this. I am not sure I understand the original question completely. When the client sends a request, it may include an "Accept-Encoding" header (format: "Accept-Encoding: gzip, deflate"). This tells the server what the client is capable of understanding. Then the server responds with a "Content-Encoding" header which tells how the page was delivered (ex: "gzip");

Code: Select all

<?php declare(encoding = 'UTF-8');  
  // create a new cURL resource
    $ch = curl_init();

  // set URL and other appropriate options
    curl_setopt($ch, CURLOPT_URL, "http://devbox/");
    curl_setopt($ch, CURLOPT_HEADER, true);
    curl_setopt($ch, CURLOPT_ENCODING, 'gzip, deflate');

  // grab URL and pass it to the browser
    curl_exec($ch);
    
  // Check if any error occured - Output Response Headers
    if(!curl_errno($ch))
     print_r(curl_getinfo($ch));
    
  // close cURL resource, and free up system resources
    curl_close($ch);

  /* Output
    HTTP/1.1 200 OK
    Date: Tue, 28 Jun 2011 02:12:35 GMT
    Server: Apache/2.2.17 (Ubuntu)
    X-Powered-By: PHP/5.2.17-0ubuntu0ppa2~maverick
    X-FRAME-OPTIONS: DENY
    Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
    Expires: Thu, 19 Nov 1981 08:52:00 GMT
    Pragma: no-cache
    Vary: Accept-Encoding
    Content-Encoding: gzip
    Content-Length: 2846
    Content-Type: text/html
  */
?>
Peter Kelly
Forum Contributor
Posts: 143
Joined: Fri Jan 14, 2011 5:33 pm
Location: England
Contact:

Re: Remote Request Headers (GZIP/DEFLATE)

Post by Peter Kelly »

Oh rite I thought the server passed over the content-encoding even if you didnt send the client encoding. Thats cleared that up, thanks.
Post Reply