Contentserv PHP API

CSHttpUtils
in package

Collection of various convenience methods around the HTTP protocol

This class offers a set of methods that will be helpful to communicate via HTTP with other servers

Tags
since
5.0.4

Table of Contents

METHOD_DELETE  = 'delete'
METHOD_GET  = 'get'
Constants for different http request methods
METHOD_HEAD  = 'head'
METHOD_OPTIONS  = 'options'
METHOD_POST  = 'post'
METHOD_PUT  = 'put'
appendFileChangedDateToUrl()  : string
Appends the file's last change date to the given URL
appendParametersToUrl()  : string
Method to append parameters to an url.
cleanHeaders()  : array<string|int, mixed>
this method removes duplicate header entries for headers prepared to be sent to the client (the headers must not be sent to the client already)
createIncludeTagsForUrlList()  : string
Creates script/link tags for scripts/stylesheets given an array of URLs
createRequest()  : CSHttpGetRequest|CSHttpPostRequest|CSHttpPutRequest|CSHttpDeleteRequest|CSHttpHeadRequest|CSHttpOptionsRequest
Creates a http request object that can be used to configre and send a request to a particular url.
DELETE()  : string
This function sends a synchronous HTTP DELETE Request
GET()  : string
This function sends a synchronous HTTP GET Request
GETAsync()  : void
This function sends an asynchronous HTTP GET Request
getClusterURL()  : string
Returns the ExternalBaseURL of the next cluster server to be able to redirect request to different servers This may e.g. be used for Imageserver URLS to be able to load more than X parallel images (corresponding to the browser settings).
getStatusCode()  : string
HEAD()  : string
This function sends a synchronous HTTP HEAD Request
OPTIONS()  : string
This function sends a synchronous HTTP OPTIONS Request
parseUrl()  : array<string|int, mixed>
parses an url and returns an array with the result (see @parse_url function for array elements)
ping()  : string
This functions send a ping to a host
POST()  : string
This function sends a synchronous HTTP POST Request
PUT()  : string
This function sends a synchronous HTTP PUT Request
redirect()  : void
this function can be used to redirect to another page
send()  : array<string|int, mixed>
Sends multiple $CSHttpRequests
setCachable()  : mixed
Marks the current reponse as cacheabel from the browser
threadResult()  : mixed
This method may be used to execute one part of a request in a separate thread and continue the main thread with its result If the inner thread need to much time and a browser timeout may occur, the process is redirected to its own url waiting until the inner thread has finished.

Constants

METHOD_DELETE

public mixed METHOD_DELETE = 'delete'
Tags

METHOD_GET

Constants for different http request methods

public mixed METHOD_GET = 'get'
Tags

METHOD_OPTIONS

public mixed METHOD_OPTIONS = 'options'
Tags

Methods

appendFileChangedDateToUrl()

Appends the file's last change date to the given URL

public static appendFileChangedDateToUrl(string $sUrl) : string
Parameters
$sUrl : string

URL to the file

Tags
Return values
string

Original URL with the file change date appended to it

appendParametersToUrl()

Method to append parameters to an url.

public static appendParametersToUrl(string $sUrl, array<string|int, CSHttpParameter$aParameters) : string
Parameters
$sUrl : string
$aParameters : array<string|int, CSHttpParameter>
Tags
Return values
string

cleanHeaders()

this method removes duplicate header entries for headers prepared to be sent to the client (the headers must not be sent to the client already)

public static cleanHeaders() : array<string|int, mixed>
Tags
access

public

Return values
array<string|int, mixed>

the new header list or null if headers could not be cleaned up

createIncludeTagsForUrlList()

Creates script/link tags for scripts/stylesheets given an array of URLs

public static createIncludeTagsForUrlList(array<string|int, mixed> $aUrls[, string $sBase = '' ]) : string
Parameters
$aUrls : array<string|int, mixed>

List of the URLs to create the tags for

$sBase : string = ''

(Optional) Path base for the URLs after they're processed

Tags
Return values
string

The created tags with the desired modifications

createRequest()

Creates a http request object that can be used to configre and send a request to a particular url.

public static createRequest(string $sUrl[, string $sMethod = self::METHOD_GET ][, int $iTimeout = 30 ]) : CSHttpGetRequest|CSHttpPostRequest|CSHttpPutRequest|CSHttpDeleteRequest|CSHttpHeadRequest|CSHttpOptionsRequest
Parameters
$sUrl : string

The url the request should be sent to.

$sMethod : string = self::METHOD_GET

The http method to use as one of the METHOD_*-constants.

$iTimeout : int = 30

The request timeout.

Tags
access

public

Return values
CSHttpGetRequest|CSHttpPostRequest|CSHttpPutRequest|CSHttpDeleteRequest|CSHttpHeadRequest|CSHttpOptionsRequest

The concrete request object based on the http method used.

DELETE()

This function sends a synchronous HTTP DELETE Request

public static DELETE(string $url[, string $user = '' ][, string $password = '' ][, int $timeout = '30' ][, bool $showError = TRUE ][, array<string|int, mixed> $cookies = NULL ][, array<string|int, mixed> $headers = NULL ]) : string
Parameters
$url : string

the URL of the request

$user : string = ''

the user name if authentication is required

$password : string = ''

the password if authentication is required

$timeout : int = '30'

the timeout of the request if the server is not accepting the connection

$showError : bool = TRUE

throw an error message, if the request is not accepted by the server

$cookies : array<string|int, mixed> = NULL

array with cookies (keys) and their values (values)

$headers : array<string|int, mixed> = NULL

array with additional headers (keys) and their values (values)

Tags
access

public

Return values
string

the result of the request if any or false if the request failed

GET()

This function sends a synchronous HTTP GET Request

public static GET(string $url[, string $user = '' ][, string $password = '' ][, int $timeout = '30' ][, bool $showError = TRUE ][, array<string|int, mixed> $cookies = NULL ][, array<string|int, mixed> $headers = NULL ]) : string
Parameters
$url : string

the URL of the request

$user : string = ''

the user name if authentication is required

$password : string = ''

the password if authentication is required

$timeout : int = '30'

the timeout of the request if the server is not accepting the connection

$showError : bool = TRUE

throw an error message, if the request is not accepted by the server

$cookies : array<string|int, mixed> = NULL

array with cookies (keys) and their values (values)

$headers : array<string|int, mixed> = NULL

array with additional headers (keys) and their values (values)

Tags
access

public

Return values
string

the result of the request if any or false if the request failed

GETAsync()

This function sends an asynchronous HTTP GET Request

public static GETAsync(string $url) : void
Parameters
$url : string

the URL of the request

Tags
access

public

Return values
void

getClusterURL()

Returns the ExternalBaseURL of the next cluster server to be able to redirect request to different servers This may e.g. be used for Imageserver URLS to be able to load more than X parallel images (corresponding to the browser settings).

public static getClusterURL() : string

Those URLS may be define in the ESA Clustermanager options

Tags
Return values
string

the next free baseURL

getStatusCode()

public static getStatusCode(int $code[, bool $includeCode = FALSE ]) : string
Parameters
$code : int

the Http Status Code like 200

$includeCode : bool = FALSE

if the code should be included in the return value again

Tags
access

public

Return values
string

the HTTP Status Code like 'OK' or '200 OK' if the $includeCode Param is set to TRUE

HEAD()

This function sends a synchronous HTTP HEAD Request

public static HEAD(string $url[, string $user = '' ][, string $password = '' ][, int $timeout = '30' ][, bool $showError = TRUE ][, array<string|int, mixed> $cookies = NULL ][, array<string|int, mixed> $headers = NULL ]) : string
Parameters
$url : string

the URL of the request

$user : string = ''

the user name if authentication is required

$password : string = ''

the password if authentication is required

$timeout : int = '30'

the timeout of the request if the server is not accepting the connection

$showError : bool = TRUE

throw an error message, if the request is not accepted by the server

$cookies : array<string|int, mixed> = NULL

array with cookies (keys) and their values (values)

$headers : array<string|int, mixed> = NULL

array with additional headers (keys) and their values (values)

Tags
access

public

Return values
string

the result of the request if any or false if the request failed

OPTIONS()

This function sends a synchronous HTTP OPTIONS Request

public static OPTIONS(string $url[, string $user = '' ][, string $password = '' ][, int $timeout = '30' ][, bool $showError = TRUE ][, array<string|int, mixed> $cookies = NULL ][, array<string|int, mixed> $headers = NULL ]) : string
Parameters
$url : string

the URL of the request

$user : string = ''

the user name if authentication is required

$password : string = ''

the password if authentication is required

$timeout : int = '30'

the timeout of the request if the server is not accepting the connection

$showError : bool = TRUE

throw an error message, if the request is not accepted by the server

$cookies : array<string|int, mixed> = NULL

array with cookies (keys) and their values (values)

$headers : array<string|int, mixed> = NULL

array with additional headers (keys) and their values (values)

Tags
access

public

Return values
string

the result of the request if any or false if the request failed

parseUrl()

parses an url and returns an array with the result (see @parse_url function for array elements)

public static parseUrl(string $url) : array<string|int, mixed>
Parameters
$url : string

the url to parse

Tags
access

public

Return values
array<string|int, mixed>

the url parts

ping()

This functions send a ping to a host

public static ping(mixed $host) : string
Parameters
$host : mixed
Tags
access

public

Return values
string

ping return string if host can be pinged, otherwise false

POST()

This function sends a synchronous HTTP POST Request

public static POST(string $url, mixed $body[, string $user = '' ][, string $password = '' ][, string $contenttype = 'application/x-www-form-urlencoded' ][, int $timeout = '30' ][, bool $showError = TRUE ][, array<string|int, mixed> $cookies = NULL ][, array<string|int, mixed> $headers = NULL ]) : string
Parameters
$url : string

the URL of the request

$body : mixed
$user : string = ''

the user name if authentication is required

$password : string = ''

the password if authentication is required

$contenttype : string = 'application/x-www-form-urlencoded'

the type of the content, e.g. application/x-www-form-urlencoded

$timeout : int = '30'

the timeout of the request if the server is not accepting the connection

$showError : bool = TRUE

throw an error message, if the request is not accepted by the server

$cookies : array<string|int, mixed> = NULL

array with cookies (keys) and their values (values)

$headers : array<string|int, mixed> = NULL

array with additional headers (keys) and their values (values)

Tags
access

public

Return values
string

the result of the request if any or false if the request failed

PUT()

This function sends a synchronous HTTP PUT Request

public static PUT(string $url, mixed $body[, string $user = '' ][, string $password = '' ][, string $contenttype = 'application/x-www-form-urlencoded' ][, int $timeout = '30' ][, bool $showError = TRUE ][, array<string|int, mixed> $cookies = NULL ][, array<string|int, mixed> $headers = NULL ]) : string
Parameters
$url : string

the URL of the request

$body : mixed

either an urlencoded string or an array containing the put parameters as key=>value pairs

$user : string = ''

the user name if authentication is required

$password : string = ''

the password if authentication is required

$contenttype : string = 'application/x-www-form-urlencoded'

the type of the content, e.g. application/x-www-form-urlencoded

$timeout : int = '30'

the timeout of the request if the server is not accepting the connection

$showError : bool = TRUE

throw an error message, if the request is not accepted by the server

$cookies : array<string|int, mixed> = NULL

array with cookies (keys) and their values (values)

$headers : array<string|int, mixed> = NULL

array with additional headers (keys) and their values (values)

Tags
access

public

Return values
string

the result of the request if any or false if the request failed

redirect()

this function can be used to redirect to another page

public static redirect(string $to[, bool $useForward = TRUE ]) : void
Parameters
$to : string

the url for the redirect

$useForward : bool = TRUE
Tags
access

public

Return values
void

send()

Sends multiple $CSHttpRequests

public static send(array<string|int, mixed> $aoCSHttpRequests) : array<string|int, mixed>
Parameters
$aoCSHttpRequests : array<string|int, mixed>

An array of CSHttpRequests to be sent concurrently

Tags
access

public

throws
CSException
Return values
array<string|int, mixed>

of CSHttpResponse An array of CSHttpResponse objects or empty array

setCachable()

Marks the current reponse as cacheabel from the browser

public static setCachable([int $seconds = NULL ]) : mixed
Parameters
$seconds : int = NULL

the seconds the response may be cached

Tags
access

public

Return values
mixed

threadResult()

This method may be used to execute one part of a request in a separate thread and continue the main thread with its result If the inner thread need to much time and a browser timeout may occur, the process is redirected to its own url waiting until the inner thread has finished.

public static threadResult(string $id[, mixed $result = NULL ]) : mixed

It may be used like: if (!($result = CSHttpUtils::threadResult('THREAD ID XXX'))) { ... do large operations having the result $result ... CSHttpUtils::threadResult('ZIP'.$this->getID(), $file); }

Parameters
$id : string

the ID of the thread which are always the same for the same GET Parameters (e.g. the IDs of objects which are selected)

$result : mixed = NULL

the optional result of the thread, if this process has been creating the result

Tags
access

public

Return values
mixed

either null, if the result must be created, or the threads result, which had been added by another thread.


        

Search results