Jan
20
HTTP Class for PHP (supports both cURL and fsockopen)
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: 983 times |
18 Comments
Make A CommentComments RSS Feed TrackBack URL


(9 votes, average: 4.33 out of 5)

























January 20th, 2008 at 10:17 pm
You can add this function in Orchid framework.
January 20th, 2008 at 10:34 pm
Yeah Omi bhai - i’ve already planned that
Just not been able to log into the SVN lately 
January 20th, 2008 at 10:47 pm
Great job Emran vai. Finally you released this.
yea its a good idea if you add this to orchid framework.
cool man
January 21st, 2008 at 12:12 am
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.
January 21st, 2008 at 12:14 am
Thanks Hasin. And yes, i would be honored if this is added to orchid.
January 21st, 2008 at 10:20 am
Wow, It’s a nice theme. Tobe menu gula dekhte ekto asubidha hocche r ki
January 21st, 2008 at 1:38 pm
Simply GENIUS!
Excellent structure, easy to use and ???????? documentation.
January 24th, 2008 at 4:40 am
[…] Allerdings kannst du das selbe auch mit PHP machen, indem du einen HTTP-request ausf?hrst. Diese Klasse kann dir dabei m?glicherweise die Arbeit vereinfachen (du kannst den get-Aufruf nat?rlich auch […]
January 25th, 2008 at 5:06 pm
[…] goes to Emran for contributing this […]
January 26th, 2008 at 11:43 pm
[…] http://www.phpfour.com/blog/2008/01/20/php-http-class/ […]
February 13th, 2008 at 11:27 pm
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.
February 13th, 2008 at 11:48 pm
@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
February 14th, 2008 at 6:00 pm
Building Spiders: Grab Data, Post Forms and Interact with Web Sites Automatically…
One of the most useful and powerful things you can do with PHP is to create a programme which will simulate a web browser and can grab data, post data to forms and generally interact with other web sites - automatically.
For PHP to be able to work like…
February 15th, 2008 at 10:44 pm
cool work.
February 27th, 2008 at 2:29 am
[…] HTTP Class for PHP - supports both cURL and fsockopen Este artículo fue posteado el Martes, Febrero 26th, 2008 a las 5:28 pm , Por Juan Jorquera , y lo encuentras en las etiquetas OpenSource, cron, php. puedes seguir los comentarios de este artículo en su feed . Puedes dejar tu comentario, o hacer un “trackback” desde tu sitio web. […]
March 12th, 2008 at 3:56 pm
Thx for this awesome class. How to make it aouto detect for curl or dsockopen ?
March 12th, 2008 at 8:39 pm
@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!
April 15th, 2008 at 6:43 pm
When use the class ,I notice
the cURL require a website need longer time then fsockect.is it?