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

_passCookies() 

Pass Cookies (internal)

Get the cookies which are valid for the current request. Checks domain and path to decide the return.

_setError(string $error) : string

Set error message (internal)

Arguments

string $error

Error message

Response

string

Error message

Properties

Contains the target URL

target : string

Type(s)

string

socket

_socket : 

Type(s)

Contains the target host

host : string

Type(s)

string

Contains the target port

port : integer

Type(s)

integer

Contains the target path

path : string

Type(s)

string

Contains the target schema

schema : string

Type(s)

string

Contains the http method (GET or POST)

method : string

Type(s)

string

Contains raw post data

rawPostData : \str

Type(s)

\str

Contains the parameters for request

params : array

Type(s)

array

Contains the cookies for request

cookies : array

Type(s)

array

Contains the cookies retrieved from response

_cookies : array

Type(s)

array

Number of seconds to timeout

timeout : integer

Type(s)

integer

Whether to use cURL or not

useCurl : boolean

Type(s)

boolean

Contains the referrer URL

referrer : string

Type(s)

string

Contains the User agent string

userAgent : string

Type(s)

string

Contains the cookie path (to be used with cURL)

cookiePath : string

Type(s)

string

Whether to use cookie at all

useCookie : boolean

Type(s)

boolean

Whether to store cookie for subsequent requests

saveCookie : boolean

Type(s)

boolean

Contains the Username (for authentication)

username : string

Type(s)

string

Contains the Password (for authentication)

password : string

Type(s)

string

Contains the fetched web source

result : string

Type(s)

string

Contains the last headers

headers : string

Type(s)

string

Contains the last call's http status code

status : string

Type(s)

string

Whether to follow http redirect or not

redirect : boolean

Type(s)

boolean

The maximum number of redirect to follow

maxRedirect : integer

Type(s)

integer

The current number of redirects

curRedirect : integer

Type(s)

integer

Contains any error occurred

error : string

Type(s)

string

Store the next token

nextToken : string

Type(s)

string

Whether to keep debug messages

debug : boolean

Type(s)

boolean

Stores optional http headers

headerArray : array

Type(s)

array

Stores the debug messages

debugMsg : array

Type(s)

array

Stores proxy information (host:port)

proxy : string

Type(s)

string