CMS Made Simple API

cms_http_request
in package

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.

Tags
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

Table of Contents

Properties

$_cookies  : array<string|int, mixed>
Contains the cookies retrieved from response
$_socket  : mixed
socket
$cookiePath  : string
Contains the cookie path (to be used with cURL)
$cookies  : array<string|int, mixed>
Contains the cookies for request
$curRedirect  : int
The current number of redirects
$debug  : bool
Whether to keep debug messages
$debugMsg  : array<string|int, mixed>
Stores the debug messages
$error  : string
Contains any error occurred
$headerArray  : array<string|int, mixed>
Stores optional http headers
$headers  : string
Contains the last headers
$host  : string
Contains the target host
$maxRedirect  : int
The maximum number of redirect to follow
$method  : string
Contains the http method (GET or POST)
$nextToken  : string
Store the next token
$params  : array<string|int, mixed>
Contains the parameters for request
$password  : string
Contains the Password (for authentication)
$path  : string
Contains the target path
$port  : int
Contains the target port
$proxy  : string
Stores proxy information (host:port)
$rawPostData  : str
Contains raw post data
$redirect  : bool
Whether to follow http redirect or not
$referrer  : string
Contains the referrer URL
$result  : string
Contains the fetched web source
$saveCookie  : bool
Whether to store cookie for subsequent requests
$schema  : string
Contains the target schema
$status  : string
Contains the last call's http status code
$target  : string
Contains the target URL
$timeout  : int
Number of seconds to timeout
$useCookie  : bool
Whether to use cookie at all
$useCurl  : bool
Whether to use cURL or not
$userAgent  : string
Contains the User agent string
$username  : string
Contains the Username (for authentication)

Methods

__construct()  : mixed
Constructor for initializing the class with default values.
_passCookies()  : mixed
Pass Cookies (internal)
addCookie()  : mixed
Add a cookie to the request
addParam()  : mixed
Add request parameters
addRequestHeader()  : mixed
Add a request header
clear()  : mixed
Clear Everything
execute()  : string
Execute a HTTP request
followRedirects()  : mixed
Whether to follow HTTP redirects
getError()  : string
Get last execution error
getHeaders()  : array<string|int, mixed>
Get execution result headers
getResult()  : string
Get execution result body
getStatus()  : int
Get execution status code
initialize()  : mixed
Initialize preferences
is_curl_suitable()  : bool
Test if the installed curl version is suitable
requestHeaderExists()  : bool
Request Header Exists?
resetCookies()  : mixed
Clear all cookies
saveCookie()  : mixed
Whether to save persistent cookies in subsequent calls
setAuth()  : mixed
Set basic http authentication realm
setCookiepath()  : mixed
Set cookie path (cURL only)
setMaxredirect()  : mixed
Set maximum number of redirection to follow
setMethod()  : mixed
Set http method
setParams()  : mixed
Set request parameters
setRawPostData()  : mixed
Set the post data string directly.
setReferrer()  : mixed
Set referrer URL
setTarget()  : mixed
Set target URL
setTimeout()  : mixed
Set timeout of execution
setUseragent()  : mixed
Set User agent string
useCookie()  : mixed
Whether to use cookies or not
useCurl()  : mixed
Whether to use cURL or not
_setError()  : string
Set error message (internal)

Properties

$_cookies

Contains the cookies retrieved from response

private array<string|int, mixed> $_cookies

$debugMsg

Stores the debug messages

private array<string|int, mixed> $debugMsg
Tags
todo

will keep debug messages

Methods

__construct()

Constructor for initializing the class with default values.

public __construct() : mixed

_passCookies()

Pass Cookies (internal)

public _passCookies() : mixed

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

Tags
access

private

addCookie()

Add a cookie to the request

public addCookie(string $name, string $value) : mixed
Parameters
$name : string

Name of cookie

$value : string

Value of cookie

addParam()

Add request parameters

public addParam(string $name, string $value) : mixed
Parameters
$name : string

Name of the parameter

$value : string

Value of the parameter

addRequestHeader()

Add a request header

public addRequestHeader(string $str[, bool $prepend = false ]) : mixed
Parameters
$str : string

The header string

$prepend : bool = false

push header on top of all other headers.

clear()

Clear Everything

public clear() : mixed

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

Execute a HTTP request

public execute([string $target = '' ][, string $referrer = '' ][, string $method = '' ][, array<string|int, mixed> $data = array() ]) : string

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

Parameters
$target : string = ''

URL of the target page (optional)

$referrer : string = ''

URL of the referrer page (optional)

$method : string = ''

The http method (GET or POST) (optional)

$data : array<string|int, mixed> = array()

Parameter array for GET or POST (optional)

Return values
string

Response body of the target page

followRedirects()

Whether to follow HTTP redirects

public followRedirects([bool $value = TRUE ]) : mixed
Parameters
$value : bool = TRUE

Whether to follow HTTP redirects or not

getError()

Get last execution error

public getError() : string
Return values
string

last error message (if any)

getHeaders()

Get execution result headers

public getHeaders() : array<string|int, mixed>
Return values
array<string|int, mixed>

last headers of execution

getResult()

Get execution result body

public getResult() : string
Return values
string

output of execution

getStatus()

Get execution status code

public getStatus() : int
Return values
int

last http status code

initialize()

Initialize preferences

public initialize([array<string|int, mixed> $config = array() ]) : mixed

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);
Parameters
$config : array<string|int, mixed> = array()

Config values as associative array

is_curl_suitable()

Test if the installed curl version is suitable

public static is_curl_suitable() : bool
Return values
bool

requestHeaderExists()

Request Header Exists?

public requestHeaderExists(string $key) : bool
Parameters
$key : string

The header key

Return values
bool

resetCookies()

Clear all cookies

public resetCookies() : mixed
Tags
author

Robert Campbell (calguy1000@gmail.com)

saveCookie()

Whether to save persistent cookies in subsequent calls

public saveCookie([bool $value = TRUE ]) : mixed
Parameters
$value : bool = TRUE

Whether to save persistent cookies or not

setAuth()

Set basic http authentication realm

public setAuth(string $username, string $password) : mixed
Parameters
$username : string

Username for authentication

$password : string

Password for authentication

setCookiepath()

Set cookie path (cURL only)

public setCookiepath(string $path) : mixed
Parameters
$path : string

File location of cookiejar

setMaxredirect()

Set maximum number of redirection to follow

public setMaxredirect(int $value) : mixed
Parameters
$value : int

Maximum number of redirects

setMethod()

Set http method

public setMethod(string $method) : mixed
Parameters
$method : string

HTTP method to use (GET or POST)

setParams()

Set request parameters

public setParams(array<string|int, mixed> $dataArray) : mixed
Parameters
$dataArray : array<string|int, mixed>

All the parameters for GET or POST

setRawPostData()

Set the post data string directly.

public setRawPostData(string $data) : mixed
Parameters
$data : string

setReferrer()

Set referrer URL

public setReferrer(string $referrer) : mixed
Parameters
$referrer : string

URL of referrer page

setTarget()

Set target URL

public setTarget(string $url) : mixed
Parameters
$url : string

URL of target resource

setTimeout()

Set timeout of execution

public setTimeout(int $seconds) : mixed
Parameters
$seconds : int

Timeout delay in seconds

setUseragent()

Set User agent string

public setUseragent(string $agent) : mixed
Parameters
$agent : string

Full user agent string

useCookie()

Whether to use cookies or not

public useCookie([bool $value = TRUE ]) : mixed
Parameters
$value : bool = TRUE

Whether to use cookies or not

useCurl()

Whether to use cURL or not

public useCurl([bool $value = TRUE ]) : mixed
Parameters
$value : bool = TRUE

Whether to use cURL or not

_setError()

Set error message (internal)

private _setError(string $error) : string
Parameters
$error : string

Error message

Tags
access

private

Return values
string

Error message


        
On this page

Search results