Services

HttpServiceInterface

v1.0.0

HTTP client abstraction for GET, POST, PUT, and DELETE requests returning decoded arrays.

Access

$ctx->http()?->get('https://api.example.com/stats');

How To Use It

  • Use it for outbound HTTP instead of wiring raw WordPress request helpers directly into module code.
  • Pair it with CacheServiceInterface when an endpoint backs dashboard widgets or settings screens that render frequently.

Methods

public function get(string $url, array $headers = []): array

Sends a GET request and returns the decoded response array.

public function post(string $url, array $body = [], array $headers = []): array

Sends a POST request with a body payload.

public function put(string $url, array $body = [], array $headers = []): array

Sends a PUT request with a body payload.

public function delete(string $url, array $headers = []): array

Sends a DELETE request.

Example

bootstrap.php
public function pushReport(SdkContext $ctx, array $payload): void
{
    $response = $ctx->http()?->post('https://api.example.com/reports', $payload, [
        'Authorization' => 'Bearer ' . ($this->getSetting('api_token') ?? ''),
    ]) ?? [];

    $ctx->logger()->info('reports.push_complete', [
        'status' => $response['status'] ?? 'unknown',
    ]);
}