abstract class Client

Client simulates a browser.

To make the actual request, you need to implement the doRequest() method.

If you want to be able to run requests in their own process (insulated flag), you need to also implement the getScript() method.

Properties

protected $history
protected $cookieJar
protected $server
protected $internalRequest
protected $request
protected $internalResponse
protected $response
protected $crawler
protected $insulated
protected $redirect
protected $followRedirects

Methods

__construct(array $server = array(), History $history = null, CookieJar $cookieJar = null)

No description

followRedirects(bool $followRedirect = true)

Sets whether to automatically follow redirects or not.

bool
isFollowingRedirects()

Returns whether client automatically follows redirects or not.

setMaxRedirects(int $maxRedirects)

Sets the maximum number of requests that crawler can follow.

int
getMaxRedirects()

Returns the maximum number of requests that crawler can follow.

insulate(bool $insulated = true)

Sets the insulated flag.

setServerParameters(array $server)

Sets server parameters.

setServerParameter(string $key, string $value)

Sets single server parameter.

string
getServerParameter(string $key, string $default = '')

Gets single server parameter for specified key.

getHistory()

Returns the History instance.

getCookieJar()

Returns the CookieJar instance.

Crawler|null
getCrawler()

Returns the current Crawler instance.

Response|null
getInternalResponse()

Returns the current BrowserKit Response instance.

object|null
getResponse()

Returns the current origin response instance.

Request|null
getInternalRequest()

Returns the current BrowserKit Request instance.

object|null
getRequest()

Returns the current origin Request instance.

click(Link $link)

Clicks on a given link.

submit(Form $form, array $values = array())

Submits a form.

request(string $method, string $uri, array $parameters = array(), array $files = array(), array $server = array(), string $content = null, bool $changeHistory = true)

Calls a URI.

object
doRequestInProcess(object $request)

Makes a request in another process.

object
doRequest(object $request)

Makes a request.

getScript(object $request)

Returns the script to execute when the request must be insulated.

object
filterRequest(Request $request)

Filters the BrowserKit request to the origin one.

filterResponse(object $response)

Filters the origin response to the BrowserKit one.

Crawler|null
createCrawlerFromContent(string $uri, string $content, string $type)

Creates a crawler.

back()

Goes back in the browser history.

forward()

Goes forward in the browser history.

reload()

Reloads the current browser.

followRedirect()

Follow redirects?

restart()

Restarts the client.

string
getAbsoluteUri(string $uri)

Takes a URI and converts it to absolute if it is not already absolute.

requestFromRequest(Request $request, bool $changeHistory = true)

Makes a request from a Request object directly.

Details

at line 52
__construct(array $server = array(), History $history = null, CookieJar $cookieJar = null)

Parameters

array $server The server parameters (equivalent of $_SERVER)
History $history A History instance to store the browser history
CookieJar $cookieJar A CookieJar instance to store the cookies

at line 64
followRedirects(bool $followRedirect = true)

Sets whether to automatically follow redirects or not.

Parameters

bool $followRedirect Whether to follow redirects

at line 74
bool isFollowingRedirects()

Returns whether client automatically follows redirects or not.

Return Value

bool

at line 84
setMaxRedirects(int $maxRedirects)

Sets the maximum number of requests that crawler can follow.

Parameters

int $maxRedirects

at line 95
int getMaxRedirects()

Returns the maximum number of requests that crawler can follow.

Return Value

int

at line 107
insulate(bool $insulated = true)

Sets the insulated flag.

Parameters

bool $insulated Whether to insulate the requests or not

Exceptions

RuntimeException When Symfony Process Component is not installed

at line 121
setServerParameters(array $server)

Sets server parameters.

Parameters

array $server An array of server parameters

at line 134
setServerParameter(string $key, string $value)

Sets single server parameter.

Parameters

string $key A key of the parameter
string $value A value of the parameter

at line 147
string getServerParameter(string $key, string $default = '')

Gets single server parameter for specified key.

Parameters

string $key A key of the parameter to get
string $default A default value when key is undefined

Return Value

string A value of the parameter

at line 157
History getHistory()

Returns the History instance.

Return Value

History A History instance

at line 167
CookieJar getCookieJar()

Returns the CookieJar instance.

Return Value

CookieJar A CookieJar instance

at line 177
Crawler|null getCrawler()

Returns the current Crawler instance.

Return Value

Crawler|null A Crawler instance

at line 187
Response|null getInternalResponse()

Returns the current BrowserKit Response instance.

Return Value

Response|null A BrowserKit Response instance

at line 202
object|null getResponse()

Returns the current origin response instance.

The origin response is the response instance that is returned by the code that handles requests.

Return Value

object|null A response instance

See also

doRequest()

at line 212
Request|null getInternalRequest()

Returns the current BrowserKit Request instance.

Return Value

Request|null A BrowserKit Request instance

at line 227
object|null getRequest()

Returns the current origin Request instance.

The origin request is the request instance that is sent to the code that handles requests.

Return Value

object|null A Request instance

See also

doRequest()

at line 237
Crawler click(Link $link)

Clicks on a given link.

Parameters

Link $link

Return Value

Crawler

at line 254
Crawler submit(Form $form, array $values = array())

Submits a form.

Parameters

Form $form A Form instance
array $values An array of form field values

Return Value

Crawler

at line 274
Crawler request(string $method, string $uri, array $parameters = array(), array $files = array(), array $server = array(), string $content = null, bool $changeHistory = true)

Calls a URI.

Parameters

string $method The request method
string $uri The URI to fetch
array $parameters The Request parameters
array $files The files
array $server The server parameters (HTTP headers are referenced with a HTTP_ prefix as PHP does)
string $content The raw body data
bool $changeHistory Whether to update the history or not (only used internally for back(), forward(), and reload())

Return Value

Crawler

at line 342
protected object doRequestInProcess(object $request)

Makes a request in another process.

Parameters

object $request An origin request instance

Return Value

object An origin response instance

Exceptions

RuntimeException When processing returns exit code

at line 361
abstract protected object doRequest(object $request)

Makes a request.

Parameters

object $request An origin request instance

Return Value

object An origin response instance

at line 370
protected getScript(object $request)

Returns the script to execute when the request must be insulated.

Parameters

object $request An origin request instance

Exceptions

LogicException When this abstract class is not implemented

at line 382
protected object filterRequest(Request $request)

Filters the BrowserKit request to the origin one.

Parameters

Request $request The BrowserKit Request to filter

Return Value

object An origin request instance

at line 394
protected Response filterResponse(object $response)

Filters the origin response to the BrowserKit one.

Parameters

object $response The origin response to filter

Return Value

Response An BrowserKit Response instance

at line 410
protected Crawler|null createCrawlerFromContent(string $uri, string $content, string $type)

Creates a crawler.

This method returns null if the DomCrawler component is not available.

Parameters

string $uri A URI
string $content Content for the crawler to use
string $type Content type

Return Value

Crawler|null

at line 427
Crawler back()

Goes back in the browser history.

Return Value

Crawler

at line 437
Crawler forward()

Goes forward in the browser history.

Return Value

Crawler

at line 447
Crawler reload()

Reloads the current browser.

Return Value

Crawler

at line 459
Crawler followRedirect()

Follow redirects?

Return Value

Crawler

Exceptions

LogicException If request was not a redirect

at line 508
restart()

Restarts the client.

It flushes history and all cookies.

at line 521
protected string getAbsoluteUri(string $uri)

Takes a URI and converts it to absolute if it is not already absolute.

Parameters

string $uri A URI

Return Value

string An absolute URI

at line 568
protected Crawler requestFromRequest(Request $request, bool $changeHistory = true)

Makes a request from a Request object directly.

Parameters

Request $request A Request instance
bool $changeHistory Whether to update the history or not (only used internally for back(), forward(), and reload())

Return Value

Crawler