\CodeIgniter\HTTPCLIRequest

Class CLIRequest

Represents a request from the command-line. Provides additional tools to interact with that request since CLI requests are not static like HTTP requests might be.

Portions of this code were initially from the FuelPHP Framework, version 1.7.x, and used here under the MIT license they were originally made available under.

http://fuelphp.com

Summary

Methods
Properties
Constants
__construct()
getIPAddress()
isValidIP()
getMethod()
setMethod()
getServer()
getEnv()
setGlobal()
fetchGlobal()
getBody()
setBody()
appendBody()
populateHeaders()
getHeaders()
getHeader()
hasHeader()
getHeaderLine()
setHeader()
removeHeader()
appendHeader()
prependHeader()
getProtocolVersion()
setProtocolVersion()
getPath()
getOptions()
getSegments()
getOption()
getOptionString()
No public properties found
No constants found
populateGlobals()
getHeaderName()
parseCommand()
$ipAddress
$proxyIPs
$method
$globals
$headers
$headerMap
$protocolVersion
$validProtocolVersions
$body
$segments
$options
N/A
No private methods found
No private properties found
N/A

Properties

$ipAddress

$ipAddress : string

IP address of the current user.

Type

string

$proxyIPs

$proxyIPs : string|array

Proxy IPs

Type

string|array

$method

$method : string

Set the expected HTTP verb

Type

string

$globals

$globals : array

Stores values we've retrieved from PHP globals.

Type

array

$headers

$headers : array

List of all HTTP request headers.

Type

array

$headerMap

$headerMap : array

Holds a map of lower-case header names and their normal-case key as it is in $headers.

Used for case-insensitive header access.

Type

array

$protocolVersion

$protocolVersion : string

Protocol version

Type

string

$validProtocolVersions

$validProtocolVersions : array

List of valid protocol versions

Type

array

$body

$body : string

Message body

Type

string

$segments

$segments : array

Stores the segments of our cli "URI" command.

Type

array

$options

$options : array

Command line options and their values.

Type

array

Methods

__construct()

__construct(\Config\App  $config) 

Constructor

Parameters

\Config\App $config

getIPAddress()

getIPAddress() : string

Gets the user's IP address.

Returns

string —

IP address

isValidIP()

isValidIP(string  $ip = null, string  $which = null) : boolean

Validate an IP address

Parameters

string $ip

IP Address

string $which

IP protocol: 'ipv4' or 'ipv6'

Returns

boolean

getMethod()

getMethod(boolean  $upper = false) : string

Get the request method.

Parameters

boolean $upper

Whether to return in upper or lower case.

Returns

string

setMethod()

setMethod(string  $method) : \CodeIgniter\HTTP\Request

Sets the request method. Used when spoofing the request.

Parameters

string $method

Returns

\CodeIgniter\HTTP\Request

getServer()

getServer(string|array|null  $index = null, integer|null  $filter = null, null  $flags = null) : mixed

Fetch an item from the $_SERVER array.

Parameters

string|array|null $index

Index for item to be fetched from $_SERVER

integer|null $filter

A filter name to be applied

null $flags

Returns

mixed

getEnv()

getEnv(null  $index = null, null  $filter = null, null  $flags = null) : mixed

Fetch an item from the $_ENV array.

Parameters

null $index

Index for item to be fetched from $_ENV

null $filter

A filter name to be applied

null $flags

Returns

mixed

setGlobal()

setGlobal(string  $method,   $value) : $this

Allows manually setting the value of PHP global, like $_GET, $_POST, etc.

Parameters

string $method
$value

Returns

$this

fetchGlobal()

fetchGlobal(string  $method, string|array|null  $index = null, integer|null  $filter = null, mixed  $flags = null) : mixed

Fetches one or more items from a global, like cookies, get, post, etc.

Can optionally filter the input when you retrieve it by passing in a filter.

If $type is an array, it must conform to the input allowed by the filter_input_array method.

http://php.net/manual/en/filter.filters.sanitize.php

Parameters

string $method

Input filter constant

string|array|null $index
integer|null $filter

Filter constant

mixed $flags

Returns

mixed

getBody()

getBody() : mixed

Returns the Message's body.

Returns

mixed

populateHeaders()

populateHeaders() 

Populates the $headers array with any headers the getServer knows about.

getHeaders()

getHeaders() : array

Returns an array containing all headers.

Returns

array —

An array of the request headers

getHeader()

getHeader(string  $name) : array|\CodeIgniter\HTTP\Header

Returns a single header object. If multiple headers with the same name exist, then will return an array of header objects.

Parameters

string $name

Returns

array|\CodeIgniter\HTTP\Header

hasHeader()

hasHeader(string  $name) : boolean

Determines whether a header exists.

Parameters

string $name

Returns

boolean

getHeaderLine()

getHeaderLine(string  $name) : string

Retrieves a comma-separated string of the values for a single header.

This method returns all of the header values of the given case-insensitive header name as a string concatenated together using a comma.

NOTE: Not all header values may be appropriately represented using comma concatenation. For such headers, use getHeader() instead and supply your own delimiter when concatenating.

Parameters

string $name

Returns

string

setHeader()

setHeader(string  $name, array|null|string  $value) : \CodeIgniter\HTTP\Message|\CodeIgniter\HTTP\Response

Sets a header and it's value.

Parameters

string $name
array|null|string $value

Returns

\CodeIgniter\HTTP\Message|\CodeIgniter\HTTP\Response

removeHeader()

removeHeader(string  $name) : \CodeIgniter\HTTP\Message

Removes a header from the list of headers we track.

Parameters

string $name

Returns

\CodeIgniter\HTTP\Message

appendHeader()

appendHeader(string  $name, string  $value) : \CodeIgniter\HTTP\Message

Adds an additional header value to any headers that accept multiple values (i.e. are an array or implement ArrayAccess)

Parameters

string $name
string $value

Returns

\CodeIgniter\HTTP\Message

prependHeader()

prependHeader(string  $name, string  $value) : \CodeIgniter\HTTP\Message

Adds an additional header value to any headers that accept multiple values (i.e. are an array or implement ArrayAccess)

Parameters

string $name
string $value

Returns

\CodeIgniter\HTTP\Message

getProtocolVersion()

getProtocolVersion() : string

Returns the HTTP Protocol Version.

Returns

string

setProtocolVersion()

setProtocolVersion(string  $version) : \CodeIgniter\HTTP\Message

Sets the HTTP protocol version.

Parameters

string $version

Returns

\CodeIgniter\HTTP\Message

getPath()

getPath() : string

Returns the "path" of the request script so that it can be used in routing to the appropriate controller/method.

The path is determined by treating the command line arguments as if it were a URL - up until we hit our first option.

Example: php index.php users 21 profile -foo bar

 // Routes to /users/21/profile (index is removed for routing sake)
 // with the option foo = bar.

Returns

string

getOptions()

getOptions() : array

Returns an associative array of all CLI options found, with their values.

Returns

array

getSegments()

getSegments() : array

Returns the path segments.

Returns

array

getOption()

getOption(string  $key) : string|null

Returns the value for a single CLI option that was passed in.

Parameters

string $key

Returns

string|null

getOptionString()

getOptionString() : string

Returns the options as a string, suitable for passing along on the CLI to other commands.

Example: $options = [ 'foo' => 'bar', 'baz' => 'queue some stuff' ];

 getOptionString() = '-foo bar -baz "queue some stuff"'

Returns

string

populateGlobals()

populateGlobals(string  $method) 

Saves a copy of the current state of one of several PHP globals so we can retrieve them later.

Parameters

string $method

getHeaderName()

getHeaderName(string  $name) : string

Takes a header name in any case, and returns the normal-case version of the header.

Parameters

string $name

Returns

string

parseCommand()

parseCommand() 

Parses the command line it was called from and collects all options and valid segments.

NOTE: I tried to use getopt but had it fail occasionally to find any options, where argv has always had our back.