PHP and HTTP making friends

Share this article

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;


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 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

Harry FuecksHarry Fuecks
View Author

Harry Fuecks is the Engineering Project Lead at Tamedia and formerly the Head of Engineering at Squirro. He is a data-driven facilitator, leader, coach and specializes in line management, hiring software engineers, analytics, mobile, and marketing. Harry also enjoys writing and you can read his articles on SitePoint and Medium.

Read Next
Get the freshest news and resources for developers, designers and digital creators in your inbox each week
Loading form