Emran Hasan

Archive of published articles on January, 2008

Back home

HTTP Class for PHP (supports both cURL and fsockopen)

20/01/2008

Feb 2009: A couple bugs have been fixed and library updated.

This is a wrapper HTTP class that uses either cURL or fsockopen to harvest resources from the web. It supports a handy subset of functionalists of HTTP that are mostly needed in day to day coding. Scripts who need to communicate with other servers will find it useful. If you’re looking to invoke any RESTful API and don’t want to bother adding a bunch of libraries for that simple thing, just put this class and you’re set.

Download

Detailed documentation can be found here. And you can download the source from here.

Update

Class added in Orchid – “PHP framwork for the rest of us”

Features

  • Can use both cURL and fsockopen.
  • Degrades to fsockopen if cURL not enabled.
  • Supports HTTP Basic authentication.
  • Supports defining custom request headers.
  • Supports defining connection timeout values.
  • Supports defining user agent and referral values.
  • Supports both user-defined and persistent cookies.
  • Supports secure connections (HTTPS) with and without cURL.
  • Supports adding requests parameters for both GET and POST.
  • Supports automatic redirection (maximum redirect can be defined).
  • Returns HTTP response headers and response body data separately.

Example 1 – Simple Get (Facebook Application List)

   1: <?php
   2:
   3: include_once('class.http.php');
   4:
   5: $http = new Http();
   6:
   7: $http->execute('http://www.facebook.com/apps/index.php?sort=6');
   8: echo ($http->error) ? $http->error : $http->result;
   9:
  10: ?>

Example 2 – Invoking Yahoo Term Extraction API

   1: <?php
   2:
   3: include_once('class.http.php');
   4:
   5: $http = new Http();
   6:
   7: $http->addParam('appid'   , 'a_really_random_yahoo_app_id');
   8: $http->addParam('context' , 'I am happy because I bought a new car');
   9: $http->addParam('output'  , 'xml');
  10:
  11: $http->execute('http://search.yahooapis.com/ContentAnalysisService/V1/termExtraction');
  12: echo ($http->error) ? $http->error : $http->result;
  13:
  14: ?>

Example 3 – Logging into Basecamp (without using cURL!)

   1: <?php
   2:
   3: include_once('class.http.php');
   4:
   5: $http = new Http();
   6:
   7: $http->useCurl(false);
   8: $http->setMethod('POST');
   9:
  10: $http->addParam('user_name', 'emran');
  11: $http->addParam('password', 'hasan');
  12:
  13: $http->setReferrer('https://someproject.projectpath.com/login');
  14: $http->execute('https://someproject.projectpath.com/login/authenticate');
  15:
  16: echo ($http->error) ? $http->error : $http->result;
  17:
  18: ?>

Example 4 – Getting a protected feed

   1: <?php
   2:
   3: include_once('class.http.php');
   4:
   5: $http = new Http();
   6: $http->setAuth('emran', 'hasan');
   7:
   8: $http->execute('http://www.someblog.com/protected/feed.xml');
   9: echo ($http->error) ? $http->error : $http->result;
  10:
  11: ?>
45 Comments

New year ahead!

20/01/2008

Although its a bit late to post a "happy new year" entry, I want to thank all the visitors who paid a visit to my blog last year and wish you a Happy New Year ahead. It’s been a great year with many ups & downs in my life, alas my short term memory won’t allow me to write a post like Hasin. Nevertheless, I am finally enjoying a planned life with time for my job, my company, my family and most important – myself !!!

Last year I set a number of goals – some were achieved and some were not. This year, I want to pressurize myself a bit by keeping those goals public in my blog. This will push me to walk in the right path as people will often ask about them and many of them won’t accept excuses. So, here goes my public commitment for 2008:

  • I will start my Masters in Business Administration (MBA)
  • I will sit (and will hopefully pass!) the Zend Certified Engineer (ZCE) exam for PHP5
  • I will write at least 4 posts per month (48 in the year – a lot considering my track record!)
  • I will be working with a number of libraries in Orchid framework
  • I will release at least one Facebook App (personally, not from my job!)

And yeah, I’ve changed the blog theme, thanks goes to Dezinerfolio – i was looking for a dark theme for a long time. I did some modification to their theme, and added a pagination like them. Hope you guys enjoy it more. Drop me a line if you have any ideas, suggestions, views, complaints – anything!

4 Comments