Classes:

Class Http

HTTP Class

This is a wrapper HTTP class that uses either cURL or fsockopen to harvest resources from web. This can be used with scripts that need a way to communicate with various APIs who support REST.

Author: Md Emran Hasan <phpfour@gmail.com>
Copyright: 2007-2008 Md Emran Hasan
Link: http://www.phpfour.com/lib/http
Since: Version 0.1
Download: here

Method Summary
void
Http ()
Constructor for initializing the class with default values.
void
addCookie ( string $name, string $value)
Add a cookie to the request
void
addParam ( string $name, string $value)
Add request parameters
void
clear ()
Clear Everything
string
execute ( string $target, string $referrer, string $method, array $data)
Execute a HTTP request
void
followRedirects ( boolean $value)
Whether to follow HTTP redirects
string
Get last execution error
array
Get execution result headers
string
Get execution result body
integer
Get execution status code
void
initialize ( array $config)
Initialize preferences
void
saveCookie ( boolean $value)
Whether to save persistent cookies in subsequent calls
void
setAuth ( string $username, string $password)
Set basic http authentication realm
void
setCookiepath ( string $path)
Set cookie path (cURL only)
void
setMaxredirect ( integer $value)
Set maximum number of redirection to follow
void
setMethod ( string $method)
Set http method
void
setParams ( array $dataArray)
Set request parameters
void
setReferrer ( string $referrer)
Set referrer URL
void
setTarget ( string $url)
Set target URL
void
setTimeout ( integer $seconds)
Set timeout of execution
void
setUseragent ( string $agent)
Set User agent string
void
useCookie ( boolean $value)
Whether to use cookies or not
void
useCurl ( boolean $value)
Whether to use cURL or not

Method Details

Http

public void Http ()

Constructor for initializing the class with default values.

addCookie

public void addCookie (string $name , string $value )

Add a cookie to the request

Input
string$nameName of cookie
string$valueValue of cookie

addParam

public void addParam (string $name , string $value )

Add request parameters

Input
string$nameName of the parameter
string$valueValue of the parameter

clear

public void clear ()

Clear Everything

Clears all the properties of the class and sets the object to the beginning state. Very handy if you are doing subsequent calls with different data.

execute

public string execute (string $target , string $referrer , string $method , array $data )

Execute a HTTP request

Executes the http fetch using all the set properties. Intellegently switch to fsockopen if cURL is not present. And be smart to follow redirects (if asked so).

Input
string$targetURL of the target page (optional)
string$referrerURL of the referrer page (optional)
string$methodThe http method (GET or POST) (optional)
array$dataParameter array for GET or POST (optional)
Output
string Response body of the target page
Exception

followRedirects

public void followRedirects (boolean $value )

Whether to follow HTTP redirects

Input
boolean$valueWhether to follow HTTP redirects or not

getError

public string getError ()

Get last execution error

Output
string last error message (if any)
Exception

getHeaders

public array getHeaders ()

Get execution result headers

Output
array last headers of execution
Exception

getResult

public string getResult ()

Get execution result body

Output
string output of execution
Exception

getStatus

public integer getStatus ()

Get execution status code

Output
integer last http status code
Exception

initialize

public void initialize (array $config )

Initialize preferences

This function will take an associative array of config values and will initialize the class variables using them.

Example use:

 $httpConfig['method']     = 'GET';
 $httpConfig['target']     = 'http://www.somedomain.com/index.html';
 $httpConfig['referrer']   = 'http://www.somedomain.com';
 $httpConfig['user_agent'] = 'My Crawler';
 $httpConfig['timeout']    = '30';
 $httpConfig['params']     = array('var1' => 'testvalue', 'var2' => 'somevalue');

 $http = new Http();
 $http->initialize($httpConfig);

Input
array$configConfig values as associative array

saveCookie

public void saveCookie (boolean $value )

Whether to save persistent cookies in subsequent calls

Input
boolean$valueWhether to save persistent cookies or not

setAuth

public void setAuth (string $username , string $password )

Set basic http authentication realm

Input
string$usernameUsername for authentication
string$passwordPassword for authentication

setCookiepath

public void setCookiepath (string $path )

Set cookie path (cURL only)

Input
string$pathFile location of cookiejar

setMaxredirect

public void setMaxredirect (integer $value )

Set maximum number of redirection to follow

Input
integer$valueMaximum number of redirects

setMethod

public void setMethod (string $method )

Set http method

Input
string$methodHTTP method to use (GET or POST)

setParams

public void setParams (array $dataArray )

Set request parameters

Input
array$dataArrayAll the parameters for GET or POST

setReferrer

public void setReferrer (string $referrer )

Set referrer URL

Input
string$referrerURL of referrer page

setTarget

public void setTarget (string $url )

Set target URL

Input
string$urlURL of target resource

setTimeout

public void setTimeout (integer $seconds )

Set timeout of execution

Input
integer$secondsTimeout delay in seconds

setUseragent

public void setUseragent (string $agent )

Set User agent string

Input
string$agentFull user agent string

useCookie

public void useCookie (boolean $value )

Whether to use cookies or not

Input
boolean$valueWhether to use cookies or not

useCurl

public void useCurl (boolean $value )

Whether to use cURL or not

Input
boolean$valueWhether to use cURL or not