cms_http_request View code

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.

Modified by Robert Campbell (calguy1000@cmsmadesimple.org) Renamed the class to cms_http_request Fixed some bugs.

package

CMS

license

GPL

author

Md Emran Hasan phpfour@gmail.com

copyright

2007-2008 Md Emran Hasan

link

http://www.phpfour.com/lib/http

since

Version 0.1

Methods

__construct() 

Constructor for initializing the class with default values.

initialize(array $config = array()) 

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);

Arguments

array $config

Config values as associative array

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.

resetCookies() 

Clear all cookies

setTarget(string $url) 

Set target URL

Arguments

string $url

URL of target resource

setMethod(string $method) 

Set http method

Arguments

string $method

HTTP method to use (GET or POST)

setReferrer(string $referrer) 

Set referrer URL

Arguments

string $referrer

URL of referrer page

setUseragent(string $agent) 

Set User agent string

Arguments

string $agent

Full user agent string

setTimeout(integer $seconds) 

Set timeout of execution

Arguments

integer $seconds

Timeout delay in seconds

setCookiepath(string $path) 

Set cookie path (cURL only)

Arguments

string $path

File location of cookiejar

setRawPostData(string $data) 

Set the post data string directly.

Arguments

string $data

setParams(array $dataArray) 

Set request parameters

Arguments

array $dataArray

All the parameters for GET or POST

setAuth(string $username, string $password) 

Set basic http authentication realm

Arguments

string $username

Username for authentication

string $password

Password for authentication

setMaxredirect(integer $value) 

Set maximum number of redirection to follow

Arguments

integer $value

Maximum number of redirects

addParam(string $name, string $value) 

Add request parameters

Arguments

string $name

Name of the parameter

string $value

Value of the parameter

addCookie(string $name, string $value) 

Add a cookie to the request

Arguments

string $name

Name of cookie

string $value

Value of cookie

useCurl(boolean $value = TRUE) 

Whether to use cURL or not

Arguments

boolean $value

Whether to use cURL or not

useCookie(boolean $value = TRUE) 

Whether to use cookies or not

Arguments

boolean $value

Whether to use cookies or not

saveCookie(boolean $value = TRUE) 

Whether to save persistent cookies in subsequent calls

Arguments

boolean $value

Whether to save persistent cookies or not

followRedirects(boolean $value = TRUE) 

Whether to follow HTTP redirects

Arguments

boolean $value

Whether to follow HTTP redirects or not

getResult() : string

Get execution result body

Response

string

output of execution

getHeaders() : array

Get execution result headers

Response

array

last headers of execution

getStatus() : integer

Get execution status code

Response

integer

last http status code

getError() : string

Get last execution error

Response

string

last error message (if any)

requestHeaderExists(string $key) : boolean

Request Header Exists?

Arguments

string $key

The header key

Response

boolean

addRequestHeader(string $str, boolean $prepend = false) 

Add a request header

Arguments

string $str

The header string

boolean $prepend

push header on top of all other headers.

is_curl_suitable() : boolean

Test if the installed curl version is suitable

static

Response

boolean

execute(string $target = '', string $referrer = '', string $method = '', array $data = array()) : string

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).

Arguments

string $target

URL of the target page (optional)

string $referrer

URL of the referrer page (optional)

string $method

The http method (GET or POST) (optional)

array $data

Parameter array for GET or POST (optional)

Response

string

Response body of the target page

Properties

Contains the target URL

target : string

Type(s)

string