LogoLogo
Frontend
  • Introduction
  • Getting started
  • About
  • Retrieving data
    • Introduction
    • Making a request
    • Property paths
    • Data providers
    • HTTP data provider
    • GraphQL data provider
    • Queries
    • GraphQL queries
    • Query Manager
    • Custom query classes
    • Bulk queries
  • Changing data
    • Introduction
    • Transforming and mapping data
    • Accessing properties
    • Transforming data
    • Available transformers
    • Mapping data
  • Advanced usage
    • Validation
    • Caching
    • Data History
    • Events
    • Testing API requests
Powered by GitBook
On this page
  • Setting up your data connection
  • Configuration
  • Authentication
  • Query String Parameters
  • Headers
  • Symfony HttpClient
  • Making requests
  • get
  • post
  • head
  • Cacheable responses
  • exists
  • getRss
  • Concurrent requests
  • Manually running concurrent requests
  • Running requests manually
  • prepareRequest
  • runRequest
  • Suppress exceptions on error

Was this helpful?

  1. Retrieving data

HTTP data provider

Setting up your data connection

Setup the Http data provider, you need to set a base URI as the first argument:

use Strata\Data\Http\Http;

$api = new Http('https://example.com/');

This then runs all future requests relative to this base URI. For example:

// Makes a request to https://example.com/my-url
$response = $api->get('my-url');

To change the base URI just use:

$api->setBaseUri('https://new.com/');

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:

$api = new Http('https://example.com/', $options);

You can also pass $options array when setting the current base URI:

$api->setBaseUri('https://example.com/', $options);

Please note this overwrites any previously set default HTTP options.

Authentication

Set basic authentication:

$api = new Http('https://example.com/', [
    'auth_basic' => 'the-username:the-password'
]);

Set a bearer authentication token:

$api = new Http('https://example.com/', [
    'auth_bearer' => 'my-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:

$api = new Http('https://example.com/', [
    'query' => [
        'auth_token' => 'ABC123'
    ]
]);

Headers

You can set any headers to send with all requests, e.g. the user agent, via the headers associative array:

$api = new Http('https://example.com/', [
    'headers' => [
        'User-Agent' => 'my-custom-application'
    ]
]);

Symfony HttpClient

$client = HttpClient::create();
$client = new ScopingHttpClient($client, [
    // the options defined as values apply only to the URLs matching
    // the regular expressions defined as keys
    'https://api\.github\.com/' => [
        'headers' => [
            'Accept' => 'application/vnd.github.v3+json',
            'Authorization' => 'token ' . $githubToken,
        ],
    ],
    // ...
]);
$api->setHttpClient($client);

Making requests

get

Run a GET request.

  • Parameters

    • string $uri URI relative to base URI

    • array $queryParams Array of query params to send with GET request

    • array $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 URI

    • array $postData Array of data to send with POST request

    • array $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 URI

    • array $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.

$response = $api->get('url-path');

if ($response->isHit()) {
    echo "HIT";
} else {
    echo "MISS";
}

See caching for more.

exists

You can use the exists() method to simply test a URL endpoint returns a 200 successful status code.

$api = new Http('https://http2.akamai.com/demo/');
$result = $api->exists('tile-101.png');
  • Parameters

    • string $uri URI relative to base URI

    • array $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.

$http = new Http('https://example.com/');

$feed = $http->getRss('feed.rss');

foreach ($feed as $item) {
    $title = $item->getTitle();
    $link = $item->getLink();
}
  • Parameters

    • string $uri URI relative to base URI

    • array $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

$uris = [
    'https://example.com/uri-1',
    'https://example.com/uri-2',
];

/** @var ResponseInterface $response */
foreach ($api->getConcurrent($uris) as $response) {
    // ... 
}

You can pass default options to use with all requests via the second parameter array $options.

foreach ($api->getConcurrent($uris, $options) as $response) { }

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).

$uris = [
    ['uri' => 'https://example.com/uri-1', 'options' => ['query' => ['page' => 1]]],
    ['uri' => 'https://example.com/uri-2', 'options' => ['query' => ['page' => 2]]],
];
foreach ($api->getConcurrent($uris) as $response) { }

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.

$api = new Http('https://http2.akamai.com/demo/');
$responses = [];
for ($i = 0; $i < 379; ++$i) {
    $uri = "tile-$i.png";
    $responses[] = $api->prepareRequest('GET', $uri);
}

foreach ($responses as $response) {
    $response = $api->runRequest($response);
    // ...
}

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 method

    • string $uri URI relative to base URI

    • array $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:

$data->suppressErrors();

Which disables exceptions for TransportExceptionInterface, HttpExceptionInterface and FailedRequestException exceptions.

You can turn off this error suppression via:

$data->suppressErrors(false);
PreviousData providersNextGraphQL data provider

Last updated 2 years ago

Was this helpful?

See for a reference for all valid options. Common options appear below.

See .

The Http data provider uses 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 to setup default options for different URL patterns:

This uses 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 and .

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 which can be looped over with foreach.

Using the example in the this can be done as so:

All requests run the prepareRequest() and runRequest(), this is to help support . You can use these methods directly, but it's recommended to use a helper method above such as get().

Symfony HttpClient configuration
Symfony HTTP authentication
Symfony HttpClient
scoping client
Laminas Feed
retrieving feed information
retrieving entry item information
generator
Symfony docs for concurrent requests
concurrent requests