PHP and HTTP making friends

If you’re looking for 15 minutes diversion, pecl_http is worthwhile, in particular the tutorial.txt file that comes with the download.

Basically gives PHP a serious HTTP boost. Some particular things it does;

  • For outgoing responses, adds a bunch of functions which reduce the effort you normally have with header e.g. http_date() for generating RFC 822/1123 dates from a Unix timestamp.
  • For incoming requests, adds other functions to make stuff you’d normally do via get_headers() easier such as http_negotiate_charset() (which might actually be useful once we hit PHP6 [PDF]).
  • Provides a wrapper round curl that has a friendly API (e.g. registers classes like HTTPRequest and HTTPMessage – API along the likes of Perl’s LWP).
  • Using a class HttpRequestPool, supports parellel requests and even has a mechanism which would allow your code to do stuff while waiting for the requests to complete. Think asynchronous XMLHttpRequests in Javascript ;) There’s a nice example of building an RSS aggregator provided, which would be another solution to Christian’s recent problem.
  • Perhaps coolest of all, provides a class HTTPResponse which seems to be closely integrated with PHP and, among other things, takes of handling HTTP caching for you (conditional GETs, ETAGs etc.

That last point is particular intriguing – it looks like you just need to active the HTTPResponse class then it automatically examines your content for you and works out what’s changed. pecl_http describes it like;

One of the main key features of HttpResponse is HTTP caching. HttpResponse will calculate an ETag based on the http.etag_mode INI setting as well as it will determine the last modification time of the sent entity. It uses those two indicators to decide if the cache entry on the client side is still valid and will emit an “304 Not Modified” response if applicable.

I’ve yet to verify this first hand (the HTTPResponse class requires PHP 5.1.x which I don’t have available right now) but the tutorial example looks like;


<?php
HttpResponse::setCacheControl('public');
HttpResponse::setCache(true);
HttpResponse::capture();

print "This will be cached until content changes!n";
print "Note that this approach will only save the clients download time.n";
?>

While this won’t save your server from the work of generating the content in the first place, it will help save your bandwidth.

Note, right now, pecl_http is not available via http://pecl4win.php.net as far as I can see, so unless you know what you’re doing, you wont be able to install it under Windows (side note: need to see if something like this works for PHP).

15 minutes are up. If you’re looking for further diversion, try HTTP Caching & Cache-Busting for Content Publishers

Free book: Jump Start HTML5 Basics

Grab a free copy of one our latest ebooks! Packed with hints and tips on HTML5's most powerful new features.

  • patrikG

    pecl_http: great potential, never heard of it. Thanks for the info.
    I’m always a wee bit careful with AJAX, live_http, etc. due to the amount of hits they generate. The caching feature on pecl_http sounds fantastic (almost too good to be true).

    Are there any known security issues or concerns so far?

  • http://www.phpism.net Maarten Manders

    Thinking of PHP and HTTP: I’ve been wondering if there’s a way to change HTTP_KEEP_ALIVE for a current connection from within PHP. Being able to lower it would help a great deal in increasing performance under heavy load.

    Or is there an Apache module to dynamically change the keepalive time depending on the httpd load?

  • http://www.phppatterns.com HarryF

    Are there any known security issues or concerns so far?

    For pecl_http? I guess one potential issue will be making sure that pages server “inside” an authenticated session don’t end up being cached by intermediate proxy servers – the problem is well described in the presentation I linked to above. Needs investigation.

    Thinking of PHP and HTTP: I’ve been wondering if there’s a way to change HTTP_KEEP_ALIVE for a current connection from within PHP. Being able to lower it would help a great deal in increasing performance under heavy load.

    I guess you could modify it with apache_setenv but whether that effect an active HTTP connection I don’t know. There’s probably some nightmares with IE + SSL as well.

    Did you see the part in pecl_http about bandwidth throttling BTW? Not directly related but gives an indication of the sort of things that work with pecl_http.

    Or is there an Apache module to dynamically change the keepalive time depending on the httpd load?

    Not that I’m aware of. Think, in general, keep alives with dynamic pages is largely unknown territory.

  • http://www.phpism.net Maarten Manders

    Here’s what i found out about the KeepAlive-Issue (tested with Apache2, PHP4 and PHP 5):

    Apache’s default KeepAlive header looks like this:
    Keep-Alive: timeout=15, max=100rn
    Which means: The connection stays open for 15 seconds after the last HTTP response. Additionally, there is a maximum of 100 subsequent requests allowed to prevent a user from keeping the connection open forever and thus hogging your server resources (RAM, that is, which is bad).

    1. Regardless of what Apache’s value of KeepAlive is, $_SERVER['HTTP_KEEP_ALIVE'] always seems to be equal ’300′. This might be a bug.

    2. I haven’t been able to change the KeepAlive header with header(‘Keep-Alive: timeout=123, max=123′).

    3. apache_setenv(‘KeepAliveTimeout’, 123) seems to be useless as well.

    4. One may change the KeepAlive in the apache config with:
    KeepAlive 123
    This accordingly changes the KeepAlive header sent to the client. However, changing the maximum requests count with
    KeepAliveTimeout 321
    doesn’t seem to change the header in any way. It stays at it’s default value (’100′). Then again I’ve seen Apache servers with a different max requests value so there must be a way to change it.

    I’ll try to investigate this further as soon as i find more time.

  • http://www.solutionsphp.com/ solutionsphp

    Sorry, this is slightly off topic… I noticed that my subscription to this feed stopped sending me new articles about a month ago. I’m using Thunderbird to manage RSS. I have tried setting up this feeds again, but Thunderbird just takes forever trying to validate the feed, never does, and I am unable to subscribe. I have validated the feed and it passes. I CAN subscribe to other SitePoint feeds, just not this one right now. Is anyone else is having trouble subscribing to this feed?

    cheers
    SAM :)

  • codestorm

    Also slightly off topic; please proofread your posts. Please.