class Browser implements ClientInterface (View source)

An HTTP client simulating a web browser

Properties

protected RequestInterface $lastRequest
protected ResponseInterface $lastResponse
protected bool $followRedirects

If redirects should be followed

protected int $maximumRedirections

The very maximum amount of redirections to follow if there is a "Location" redirect (see also $redirectionStack property)

protected array $redirectionStack

A simple string array that keeps track of occurred "Location" header redirections to avoid infinite loops if the same redirection happens

protected Headers $automaticRequestHeaders
protected RequestEngineInterface $requestEngine
protected ServerRequestFactoryInterface $serverRequestFactory
protected StreamFactoryInterface $contentStreamFactory

Methods

__construct()

Construct the Browser instance.

void
setRequestEngine(RequestEngineInterface $requestEngine)

Inject the request engine

void
addAutomaticRequestHeader(string $name, array|string|DateTime $values)

Allows to add headers to be sent with every request the browser executes.

void
removeAutomaticRequestHeader(string $name)

Allows to remove headers that were added with addAutomaticRequestHeader.

ResponseInterface
request(string|UriInterface $uri, string $method = 'GET', array $arguments = [], array $files = [], array $server = [], string $content = null)

Requests the given URI with the method and other parameters as specified.

void
setFollowRedirects(bool $flag)

Sets a flag if redirects should be followed or not.

ResponseInterface
sendRequest(RequestInterface $request)

Sends a prepared request and returns the respective response.

ResponseInterface
getLastResponse()

Returns the response received after the last request.

RequestInterface
getLastRequest()

Returns the last request executed.

getRequestEngine()

Returns the request engine used by this Browser.

Crawler
getCrawler()

Returns the DOM crawler which can be used to interact with the web page structure, submit forms, click links or fetch specific parts of the website's contents.

Form
getForm(string $xpath = '//form')

Get the form specified by $xpath. If no $xpath given, return the first form on the page.

ResponseInterface
submit(Form $form)

Submit a form

Details

__construct()

Construct the Browser instance.

void setRequestEngine(RequestEngineInterface $requestEngine)

Inject the request engine

Parameters

RequestEngineInterface $requestEngine

Return Value

void

void addAutomaticRequestHeader(string $name, array|string|DateTime $values)

Allows to add headers to be sent with every request the browser executes.

Parameters

string $name

Name of the header, for example "Location", "Content-Description" etc.

array|string|DateTime $values

An array of values or a single value for the specified header field

Return Value

void

See also

\Neos\Flow\Http\Client\Message::setHeader()

void removeAutomaticRequestHeader(string $name)

Allows to remove headers that were added with addAutomaticRequestHeader.

Parameters

string $name

Name of the header, for example "Location", "Content-Description" etc.

Return Value

void

ResponseInterface request(string|UriInterface $uri, string $method = 'GET', array $arguments = [], array $files = [], array $server = [], string $content = null)

Requests the given URI with the method and other parameters as specified.

If a Location header was given and the status code is of response type 3xx (see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html, 14.30 Location)

Parameters

string|UriInterface $uri
string $method

Request method, for example "GET"

array $arguments

Arguments to send in the request body

array $files

A (deep) array of UploadedFile or an untangled $_FILES array

array $server
string $content

Return Value

ResponseInterface

The HTTP response

Exceptions

InvalidArgumentException
InfiniteRedirectionException

void setFollowRedirects(bool $flag)

Sets a flag if redirects should be followed or not.

Parameters

bool $flag

Return Value

void

ResponseInterface sendRequest(RequestInterface $request)

Sends a prepared request and returns the respective response.

Parameters

RequestInterface $request

Return Value

ResponseInterface

Exceptions

ClientExceptionInterface

ResponseInterface getLastResponse()

Returns the response received after the last request.

Return Value

ResponseInterface

The HTTP response or NULL if there wasn't a response yet

RequestInterface getLastRequest()

Returns the last request executed.

Return Value

RequestInterface

The HTTP request or NULL if there wasn't a request yet

RequestEngineInterface getRequestEngine()

Returns the request engine used by this Browser.

Return Value

RequestEngineInterface

Crawler getCrawler()

Returns the DOM crawler which can be used to interact with the web page structure, submit forms, click links or fetch specific parts of the website's contents.

The returned DOM crawler is bound to the response of the last executed request.

Return Value

Crawler

Form getForm(string $xpath = '//form')

Get the form specified by $xpath. If no $xpath given, return the first form on the page.

Parameters

string $xpath

Return Value

Form

ResponseInterface submit(Form $form)

Submit a form

Parameters

Form $form

Return Value

ResponseInterface