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 | |
since |
Version 0.1 |
__construct()
initialize(array $config = array())
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);
array
$configConfig values as associative array
clear()
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()
setTarget(string $url)
string
$urlURL of target resource
setMethod(string $method)
string
$methodHTTP method to use (GET or POST)
setReferrer(string $referrer)
string
$referrerURL of referrer page
setUseragent(string $agent)
string
$agentFull user agent string
setTimeout(integer $seconds)
integer
$secondsTimeout delay in seconds
setCookiepath(string $path)
string
$pathFile location of cookiejar
setRawPostData(string $data)
string
$datasetParams(array $dataArray)
array
$dataArrayAll the parameters for GET or POST
setAuth(string $username, string $password)
string
$usernameUsername for authentication
string
$passwordPassword for authentication
setMaxredirect(integer $value)
integer
$valueMaximum number of redirects
addParam(string $name, string $value)
string
$nameName of the parameter
string
$valueValue of the parameter
addCookie(string $name, string $value)
string
$nameName of cookie
string
$valueValue of cookie
useCurl(boolean $value = TRUE)
boolean
$valueWhether to use cURL or not
useCookie(boolean $value = TRUE)
boolean
$valueWhether to use cookies or not
saveCookie(boolean $value = TRUE)
boolean
$valueWhether to save persistent cookies or not
followRedirects(boolean $value = TRUE)
boolean
$valueWhether to follow HTTP redirects or not
getResult() : string
string
output of execution
getHeaders() : array
array
last headers of execution
getStatus() : integer
integer
last http status code
getError() : string
string
last error message (if any)
requestHeaderExists(string $key) : boolean
string
$keyThe header key
boolean
addRequestHeader(string $str, boolean $prepend = false)
string
$strThe header string
boolean
$prependpush header on top of all other headers.
is_curl_suitable() : boolean
boolean
execute(string $target = '', string $referrer = '', string $method = '', array $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).
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)
string
Response body of the target page
_passCookies()
Get the cookies which are valid for the current request. Checks domain and path to decide the return.
_setError(string $error) : string
string
$errorError message
string
Error message
target : string
string
_socket :
host : string
string
port : integer
integer
path : string
string
schema : string
string
method : string
string
rawPostData : \str
\str
params : array
array
timeout : integer
integer
useCurl : boolean
boolean
referrer : string
string
userAgent : string
string
useCookie : boolean
boolean
saveCookie : boolean
boolean
username : string
string
password : string
string
result : string
string
headers : string
string
status : string
string
redirect : boolean
boolean
maxRedirect : integer
integer
curRedirect : integer
integer
error : string
string
nextToken : string
string
debug : boolean
boolean
headerArray : array
array
debugMsg : array
array
proxy : string
string