HTTP Class for PHP (supports both cURL and fsockopen)

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.

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

Download

class.http.php
HTTP Class for PHP (supports both cURL and fsockopen)
Downloaded: 4231 times
Share and Enjoy:
  • Digg
  • DZone
  • Twitter
  • Posterous
  • Reddit
  • del.icio.us
  • StumbleUpon
  • Technorati
  • Facebook
  • Google Bookmarks
  • LinkedIn
  • Simpy
  • Ping.fm
  • Tumblr
Post rating
1 Star2 Stars3 Stars4 Stars5 Stars (18 votes, average: 4.67 out of 5)
Loading ... Loading ...

  • That is one fine piece of code. I am very new to PHP and that will make my life so much simpler. I just have one question, How do i go about using proxies in it? Sorry if it is right in front of me but I have only been trying my hand a PHP for a few weeks now.
  • Thanks for your comment. Unfortunately, proxies have not been implemented yet. Hope to add soon.
  • sue
    Hi Emran,

    Thanks for this class. It definetly makes code simple. Please see my code below(just used your example with little tweaks).

    I have couple of questions:

    1) We have special chars in password. Can i use it directly.
    2) We have SSO authentication. After authenticating it will forward to index.jsp page. When I use the below code, even though I specify userId/pswd, it is not authenticating. It is stopping at the login page. Can you please guide me how to come over this. Thanks.

    $http = new Http();
    $http->useCurl(false);
    $http->setMethod('GET');
    $http->addParam('user_name', 'test.id');
    $http->addParam('password', 'test!!pwd');
    $http->followRedirects(true);
    //$http->setReferrer('https://www.test.com/suite/portal/index.jsp');
    $http->execute('https://www.test.com/suite/portal/index.jsp');

    echo ($http->error) ? $http->error : $http->result;
  • Thanks for your comment. Special character on password should work okay as I've done so before. Regarding your login issue, I'm afraid it needs to be inspected more closely. Email me if you think you need my help on this.
  • Thanks for making this useful class available. I was wondering if there's a way to download a zip file using this utility - to be unzipped later.
  • Any reason the redirects on fsockopen route don't support 301 redirects?

    --snip--
    if ($this->status == '302' && $this->redirect == TRUE)
    --snip--
  • Fixed. Thanks for noticing.
  • Rob
    Could you please show me an example of the usage of cookies inside this class?
  • Yes, it's similar to how you add a param:
    $http->addCookie('logged_in_user' , '747');
  • pwb
    What's the point of cURL if fsockopen works?
blog comments powered by Disqus