HTTP data provider
Setting up your data connection
Setup the Http data provider, you need to set a base URI as the first argument:
This then runs all future requests relative to this base URI. For example:
To change the base URI just use:
Configuration
You can configure the data provider with default HTTP options in a number of ways.
You can pass $options
array when creating a new Http
object:
You can also pass $options
array when setting the current base URI:
Please note this overwrites any previously set default HTTP options.
Authentication
Set basic authentication:
Set a bearer authentication token:
It's also common for APIs to also set API tokens via query string parameters (see below).
Query String Parameters
You can set any query string params to send with all requests, e.g. an auth token, via the query
associative array:
Headers
You can set any headers to send with all requests, e.g. the user agent, via the headers
associative array:
Symfony HttpClient
Making requests
get
Run a GET request.
Parameters
string $uri
URI relative to base URIarray $queryParams
Array of query params to send with GET requestarray $options
HTTP options to use for this request (these override default HTTP options)
Returns an object of type
Strata\Data\Http\Response\CacheableResponse
post
Run a POST request.
Parameters
string $uri
URI relative to base URIarray $postData
Array of data to send with POST requestarray $options
HTTP options to use for this request (these override default HTTP options)
Returns an object of type
Strata\Data\Http\Response\CacheableResponse
head
Run a HEAD request.
Parameters
string $uri
URI relative to base URIarray $options
HTTP options to use for this request (these override default HTTP options)
Returns an object of type
Strata\Data\Http\Response\CacheableResponse
Cacheable responses
Most requests return responses as objects of type CacheableResponse
. These are identical to the standard Symfony response object with one additional method isHit()
which lets you know whether this response was returned from the cache or run live.
See caching for more.
exists
You can use the exists()
method to simply test a URL endpoint returns a 200 successful status code.
Parameters
string $uri
URI relative to base URIarray $options
HTTP options to use for this request (these override default HTTP options)
Returns a boolean
getRss
You can use the getRss()
method to retrieve and decode an RSS feed.
Parameters
string $uri
URI relative to base URIarray $options
HTTP options to use for this request (these override default HTTP options)
Returns an iterable object of type
Laminas\Feed\Reader\Feed\FeedInterface
Concurrent requests
You can pass default options to use with all requests via the second parameter array $options
.
If each request needs custom options (e.g. query params) then you can pass an array of arrays, with each request having two keys (uri
, options
).
If any default options are passed to getConcurrent()
these are used with requests and merged with any request options set for individual requests. Please note, options in individual requests override default options.
Manually running concurrent requests
You can also manually run concurrent requests by making a request in two steps: first prepare the request, then run it.
The runRequest()
method checks the status code to ensure the request has run successfully.
Running requests manually
prepareRequest
Prepare request (but do not run it).
Parameters
string $method
HTTP methodstring $uri
URI relative to base URIarray $options
HTTP options to use for this request (these override default HTTP options)
Returns an object of type
Strata\Data\Http\Response\CacheableResponse
runRequest
Run a request (note Symfony HttpClient is lazy loading so this still won't actually run a HTTP request until content is accessed).
Parameters
CacheableResponse $response
Response to run
Returns an object of type
Strata\Data\Http\Response\CacheableResponse
Suppress exceptions on error
The default behaviour of is to throw exceptions on HTTP or JSON decoding errors (e.g. if a request does not return a 200 status), though this can be suppressed. It can be useful to do this for sub-requests which you don't want to stop the master HTTP request.
You can do this via:
Which disables exceptions for TransportExceptionInterface
, HttpExceptionInterface
and FailedRequestException
exceptions.
You can turn off this error suppression via:
Last updated
Was this helpful?