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: 4914 times |

Hello, this is Emran. This is my blog where I (ir) regularly express my ideas and views regarding programming, life, work, and so on. 
20/01/2008 at 10:17 pm Permalink
You can add this function in Orchid framework.
20/01/2008 at 10:34 pm Permalink
Yeah Omi bhai – i’ve already planned that
Just not been able to log into the SVN lately
20/01/2008 at 10:47 pm Permalink
Great job Emran vai. Finally you released this.
yea its a good idea if you add this to orchid framework.
cool man
21/01/2008 at 12:12 am Permalink
Well done Batman! A very good one. I was thinking that we need such a class library for orchid. Can I please add your class with default distribution?
You are doing excellent. Keep up the good work. Godspeed.
21/01/2008 at 12:14 am Permalink
Thanks Hasin. And yes, i would be honored if this is added to orchid.
21/01/2008 at 10:20 am Permalink
Wow, It’s a nice theme. Tobe menu gula dekhte ekto asubidha hocche r ki
21/01/2008 at 1:38 pm Permalink
Simply GENIUS!
Excellent structure, easy to use and ???????? documentation.
13/02/2008 at 11:27 pm Permalink
Thanks for the code. I found it in the Code Igniter forums. I turned it into a library for use in my most recent web app.
13/02/2008 at 11:48 pm Permalink
@jack: Great to know that it came to help you exactly in the manner i expected it would. Great to know that you’re using it in your project
15/02/2008 at 10:44 pm Permalink
cool work.
12/03/2008 at 3:56 pm Permalink
Thx for this awesome class. How to make it aouto detect for curl or dsockopen ?
12/03/2008 at 8:39 pm Permalink
@Andy Gelox: You’re welcome. The class tries to use cURL by default, but if cURL is not installed, it automatically falls back to fsockopen. And you can specify by yourself in case you need to. Cheers!
15/04/2008 at 6:43 pm Permalink
When use the class ,I notice
the cURL require a website need longer time then fsockect.is it?
27/06/2008 at 4:37 pm Permalink
Hi,
with allow redirect set to true if i call a page with status code 302 i can’t read the headers of destination page, i read only the headers of first page.
it’s normal?
can you help me?
Thanks
ciao
riccardo
07/10/2008 at 3:13 pm Permalink
Thanks for sharing the class.
It is quite helpful in php4 and php5. Love how it degrades automatically if there is no CURL. Have you though of supporting stream wrappers?
09/10/2008 at 2:08 am Permalink
It seems that fsockopen method doesn't follow redirects.
11/10/2008 at 9:53 am Permalink
Well, it should follow redirects with or without cURL. I will have a look
into it and will update the source if needed. Thanks
27/11/2008 at 4:35 pm Permalink
Is it possible with this class to post data, so they can be fetch on the other side with: file_get_contents(“php://input”)?
13/12/2008 at 1:03 pm Permalink
What's the point of cURL if fsockopen works?
28/01/2009 at 2:05 pm Permalink
Any reason the redirects on fsockopen route don't support 301 redirects?
–snip–
if ($this->status == '302' && $this->redirect == TRUE)
–snip–
18/02/2009 at 1:44 pm Permalink
Fixed. Thanks for noticing.
18/02/2009 at 1:47 pm Permalink
Yes, it's similar to how you add a param:
$http->addCookie('logged_in_user' , '747');18/02/2009 at 1:49 pm Permalink
The library supports both, so it's the choice of the dev
22/04/2009 at 8:03 pm Permalink
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.
21/05/2009 at 12:47 pm Permalink
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;
29/06/2009 at 4:33 am Permalink
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.
21/08/2009 at 11:48 pm Permalink
Thanks for your comment. Unfortunately, proxies have not been implemented yet. Hope to add soon.
21/08/2009 at 11:50 pm Permalink
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.
23/04/2010 at 2:21 am Permalink
Hey. I’ve been trying out your class, but i get an error on most websites:
“501 Not Implemented” or “404 Bad Request” with the body “Method Not Implemented”.
I get that last error in your own test_fbapp.php script.
I do not know why i get this. Do you?
Same error in both PHP 5.3.0 and PHP 5.2.11.
23/04/2010 at 2:25 am Permalink
Oh. And i also get a few warnings. Mostly “Undefined variable: cookieString”, also “Undefined index: transfer-encoding” etc….
23/04/2010 at 5:29 am Permalink
@MiniGod – The class has not been updated for a long time and does have a few more issues. I’d suggest you use the PECL HTTP extension – it’s a good one and these days I use that too.
07/07/2010 at 9:10 am Permalink
i love this bog, i just bookmarked it to my delicious, thank you for posting this.