> For the complete documentation index, see [llms.txt](https://php-fhir-tools.ardenexal.net/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://php-fhir-tools.ardenexal.net/fhir-http-client/overview.md). Agents can also search and read this documentation through its [MCP server](https://php-fhir-tools.ardenexal.net/~gitbook/mcp).

# Overview

Call a FHIR server from PHP, with typed search results, OAuth and x-fhir-query templates.

`ardenexal/fhir-http-client` is the toolkit's client for talking to a FHIR server. Other packages make their FHIR server calls through it:

* SDC `$populate` fetches live `application/x-fhir-query` context with it. See [SDC](https://php-fhir-tools.ardenexal.net/sdc/overview.md).
* FHIRPath `resolve()` uses it for relative and canonical references, and `memberOf()` for value-set checks. A reference's own absolute URL is fetched with a separate PSR-18 client instead. See [FHIR-Specific functions](https://php-fhir-tools.ardenexal.net/fhirpath/functions/fhir.md).
* The HTTP terminology clients for binding validation live in this package. See [Validation configuration](https://php-fhir-tools.ardenexal.net/validation/configuration.md).

```bash
composer require ardenexal/fhir-http-client symfony/http-client
```

The package depends only on the Symfony HTTP client contracts. The examples on this page use `symfony/http-client` as the implementation; any `HttpClientInterface` implementation works.

No FHIR server is called unless you construct a `FHIRHttpClient` with a real HTTP client and pass it in.

## `FHIRHttpClient`

`FHIRHttpClient` wraps a Symfony `HttpClientInterface` and one server base URL. It implements `FHIRHttpClientInterface` from `fhir-metadata`:

| Method                                                                             | Returns                                                                      |
| ---------------------------------------------------------------------------------- | ---------------------------------------------------------------------------- |
| `search(string $search, string $fhirVersion)`                                      | The search result as a typed `Bundle` model for `'R4'`, `'R4B'` or `'R5'`    |
| `followLink(string $url, string $fhirVersion)`                                     | The page a `Bundle.link.url` points at, as a typed `Bundle`                  |
| `request(string $method, string $path, ?string $body = null, array $headers = [])` | The raw response body, for any other FHIR REST call such as `$validate-code` |

Each method returns `null` when the call fails: a transport error, a timeout, a non-2xx status or a body it cannot read. `search()` and `followLink()` throw a `\ValueError` for an unknown FHIR version, because that is a programming error rather than a server failure.

```php
use Ardenexal\FHIRTools\Component\HttpClient\FHIRHttpClient;
use Symfony\Component\HttpClient\HttpClient;

$client = new FHIRHttpClient(HttpClient::create(), 'https://fhir.example.org/r4');

$bundle = $client->search('Observation?subject=Patient/123&status=final', 'R4');
if ($bundle === null) {
    // The server was unreachable, timed out, answered 4xx/5xx, or returned a body that is not a Bundle
}
```

The search string is always joined onto the configured base URL, so it can only become the path and query of the request, never a different host.

### Following result pages

A search result that spans several pages carries a `Bundle.link` with `relation = 'next'`. Pass its URL to `followLink()` to fetch the next page:

```php
$page = $client->search('Observation?subject=Patient/123', 'R4');
while ($page !== null) {
    // ... read $page's entries ...
    $nextUrl = /* the url of $page's link where relation = 'next', or null */;
    $page    = $nextUrl !== null ? $client->followLink($nextUrl, 'R4') : null;
}
```

`followLink()` follows only URLs on the client's own server: the scheme, host and port must match the base URL, and the path must sit under the base URL's path. Any other URL returns `null`, so a server cannot point the client at another host.

### Authentication

`FHIRHttpClient` sends every request through the Symfony client you give it, so configure authentication on that client:

```php
$authed = HttpClient::create()->withOptions(['auth_bearer' => $token]); // or 'auth_basic' => 'user:pass'
$client = new FHIRHttpClient($authed, 'https://fhir.example.org/r4');
```

Two decorators in the `OAuth\` namespace cover the cases `withOptions()` does not:

| Class                                                                    | Use it for                                                                                                                                                                                                        |
| ------------------------------------------------------------------------ | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `OAuth\OAuthHttpClient` with `OAuth\OAuthClientCredentialsTokenProvider` | An OAuth 2.0 client-credentials token fetched from a token endpoint and reused until 60 seconds before the server's `expires_in`. Pass a PSR-6 pool to the provider to share the token between provider instances |
| `OAuth\StaticHeaderHttpClient`                                           | One fixed header on every request, such as a token you already hold or `X-Api-Key`                                                                                                                                |

```php
use Ardenexal\FHIRTools\Component\HttpClient\OAuth\OAuthClientCredentialsTokenProvider;
use Ardenexal\FHIRTools\Component\HttpClient\OAuth\OAuthHttpClient;
use Ardenexal\FHIRTools\Component\HttpClient\OAuth\StaticHeaderHttpClient;

$http = HttpClient::create();

$tokens = new OAuthClientCredentialsTokenProvider($http, $tokenUrl, $clientId, $clientSecret);
$client = new FHIRHttpClient(new OAuthHttpClient($http, $tokens), 'https://fhir.example.org/r4');

$client = new FHIRHttpClient(
    new StaticHeaderHttpClient($http, 'X-Api-Key', $apiKey),
    'https://fhir.example.org/r4',
);
```

A provider reuses its token until 60 seconds before the server's `expires_in`, then fetches a new one, so a client that lives longer than one token, such as a Messenger consumer, keeps working. A token whose `expires_in` is 60 seconds or less is used for one request only. To control the time the provider checks against, pass a PSR-20 clock as its `clock` argument.

When a server answers `401`, `OAuthHttpClient` drops the token and sends the request once more with a new one. A second `401` is returned as is. A body that cannot be sent twice, such as a stream, a generator or a form upload holding a stream, is not resent, but the token is still dropped so the next request gets a new one. Set body defaults through `OAuthHttpClient::withOptions()`, not on the client it wraps: a default set on the wrapped client can't be seen, so its body is treated as safe to resend. To read the status, `request()` waits for the response headers before it returns.

When a token cannot be fetched, the request fails like any other transport error, so the `FHIRHttpClient` method returns `null`. The error message never contains the client secret.

## `NullFHIRHttpClient`

`NullFHIRHttpClient` makes no requests and returns `null` from every method. Use it where a client is required but no server is configured. The Symfony bundle registers it as the default `FHIRHttpClientInterface`. To use a real server, install `symfony/http-client` so the bundle can autowire an `HttpClientInterface`, then register `FHIRHttpClient` in your application's `services.yaml` and point the alias at it:

```yaml
services:
    Ardenexal\FHIRTools\Component\HttpClient\FHIRHttpClient:
        arguments:
            $baseUrl: '%env(FHIR_SERVER_URL)%'
    Ardenexal\FHIRTools\Component\Metadata\Contract\FHIRHttpClientInterface:
        alias: 'Ardenexal\FHIRTools\Component\HttpClient\FHIRHttpClient'
```

## Resolving x-fhir-query templates

`XFhirQuery\XFhirQueryResolver` turns an [x-fhir-query](https://hl7.org/fhir/R5/fhir-xquery.html) template into a search string. Each `{{ … }}` hole is a FHIRPath expression evaluated against an `EvaluationContext`, with launch-context resources bound as external constants such as `%patient`. The resolver makes no request; pass its output to `search()`.

```php
use Ardenexal\FHIRTools\Component\FHIRPath\Evaluator\EvaluationContext;
use Ardenexal\FHIRTools\Component\HttpClient\XFhirQuery\XFhirQueryResolver;

$context = (new EvaluationContext())->withExternalConstant('patient', $patient);
$search  = (new XFhirQueryResolver())->resolve('Observation?subject={{%patient.id}}&status=final', $context);
// 'Observation?subject=123&status=final'

$bundle = $client->search($search, 'R4');
```

Substituted values are percent-encoded, except a token's system URI and the `|` and `,` separators, which are emitted as written. A search parameter whose hole resolves to nothing is dropped from the query, which widens the search: `Observation?subject={{%patient.id}}` with a `%patient` that has no `id` becomes `Observation`, a search for every subject's observations. When every parameter is dropped, the `?` is dropped too. An unterminated `{{` throws `\InvalidArgumentException`, and a hole naming a constant the context does not bind throws the FHIRPath `EvaluationException`.

{% hint style="warning" %}
The client returns whatever the server sends for a search. It applies no access control, so deciding what a user may see, and constraining the query or launch context to match, is up to the caller.
{% endhint %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://php-fhir-tools.ardenexal.net/fhir-http-client/overview.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
