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.
See Symfony HttpClient configuration for a reference for all valid options. Common options appear below.
Authentication
Set basic authentication:
Set a bearer authentication token:
See Symfony HTTP authentication.
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
The Http
data provider uses Symfony HttpClient to make requests. This is automatically created when it's needed. If you need to, you can set this up yourself and set via setHttpClient()
.
An example usage would be if you want to set up a scoping client to setup default options for different URL patterns:
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
This uses Laminas Feed to decode the RSS feed, this supports RSS and Atom feeds of any version, including RDF/RSS 1.0, RSS 2.0, Atom 0.3, and Atom 1.0.
See docs on retrieving feed information and retrieving entry item information.
Concurrent requests
You can run a bulk set of GET requests quickly and efficiently by passing an array of URIs to the getConcurrent()
method. This returns a generator which can be looped over with foreach
.
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.
Using the example in the Symfony docs for concurrent requests this can be done as so:
The runRequest()
method checks the status code to ensure the request has run successfully.
Running requests manually
All requests run the prepareRequest()
and runRequest()
, this is to help support concurrent requests. You can use these methods directly, but it's recommended to use a helper method above such as get()
.
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