Api
class Api
Lightweight HTTP JSON client with optional on-disk caching.
Responsibilities:
- Build URLs from a base URL plus default and per-call query parameters.
- Perform JSON-based GET/POST requests with cURL and sensible timeouts.
- Cache successful GET responses to
storage/cache/<namespace>/cache_<hash>.json.
Typical usage is to subclass this client and provide service-specific defaults
(e.g., base URL, default headers, API key in $defaultQuery, TTL, etc.).
Constants
| private CACHE_DIRECTORY |
Root cache directory for the application. |
Properties
| protected string | $baseUrl | Base URL (no trailing slash), e.g. "https://api.example.com/v1" |
|
| protected string | $cacheDir | Absolute path to the cache directory for this client/namespace. |
|
| protected array<string,string> | $defaultHeaders | Default headers sent on every request (merged with per-call headers). |
|
| protected array<string,mixed> | $defaultQuery | Default query parameters merged into every request (per-call overrides). |
|
| protected int | $defaultTtl | Default TTL (seconds) for GET cache. 0 disables caching. |
|
| protected int | $timeout | Connection/overall timeout in seconds. |
Methods
Constructor for Api object.
Build an absolute URL with merged default + per-call query parameters.
Compute the cache filename for a given URL.
Convert an associative array of headers into "Key: Value" lines.
Perform a GET request that expects a JSON response.
Perform a POST request with a JSON body and expect a JSON response.
Read a cached JSON response for a URL if it exists and is fresh.
Core HTTP request using cURL that decodes JSON and throws on errors.
Persist a decoded JSON response to the cache for a given URL.
Details
at line 72
__construct(string $baseUrl, string $cacheNamespace = 'api', array $defaultHeaders = ['Accept' => 'application/json'], array $defaultQuery = [], int $defaultTtl = 0, int $timeout = 6)
Constructor for Api object.
at line 99
protected string
buildUrl(string $path, array $query)
Build an absolute URL with merged default + per-call query parameters.
Per-call parameters override defaults on conflict.
at line 111
protected string
cacheFile(string $url)
Compute the cache filename for a given URL.
at line 121
protected array
flattenHeaders(array $headers)
Convert an associative array of headers into "Key: Value" lines.
at line 141
array
get(string $path, array $query = [], int|null $ttl = null)
Perform a GET request that expects a JSON response.
Utilizes on-disk caching when TTL > 0.
at line 169
array
post(string $path, array $body = [], array $query = [], array $headers = [])
Perform a POST request with a JSON body and expect a JSON response.
at line 188
protected array|null
readCache(string $url, int $ttl)
Read a cached JSON response for a URL if it exists and is fresh.
at line 209
protected array
requestJson(string $method, string $url, string|null $body, array $headers)
Core HTTP request using cURL that decodes JSON and throws on errors.
at line 255
protected void
writeCache(string $url, array $data)
Persist a decoded JSON response to the cache for a given URL.