HTTP Client

Nextcloud comes with a simple HTTP client that can be used to send requests to other web servers. This client follows Nextcloud’s configuration, security restrictions and proxy settings.

Acquiring a HTTP Client

HTTP client instances are built using the client service factory IClientService. The factory can be injected into any app class:

<?php

use OCP\Http\Client\IClientService;

class MyRemoteServerIntegration {
    private IClientService $clientService;
    public function __construct(IClientService $clientService) {
        $this->clientService = $clientService;
    }

    public function downloadNextcloudWebsite(): void {
        $client = $this->clientService->newClient();
        $response = $client->get('https://nextcloud.com');
        $body = $response->getBody();
    }
}

HEAD request

<?php

$client = $this->clientService->newClient();
$response = $client->head('https://nextcloud.com');
$body = $response->getBody();

GET request

<?php

$client = $this->clientService->newClient();
$response = $client->get('https://nextcloud.com');
$body = $response->getBody();

POST request

<?php

$client = $this->clientService->newClient();
$response = $client->post('https://api.domain.tld/pizza', [
    'headers' => [
        'Accept' => 'application/json',
        'Content-Type' => 'application/json',
    ],
    'body' => json_encode([
        'toppings' => [
            'cheese',
            'pineapple',
        ],
    ])
]);
$pizza = json_decode($response->getBody(), true);

PUT request

<?php

$client = $this->clientService->newClient();
$response = $client->put('https://api.domain.tld/pizza/42', [
    'headers' => [
        'Accept' => 'application/json',
        'Content-Type' => 'application/json',
    ],
    'body' => json_encode([
        'toppings' => [
            'cheese',
            'pineapple',
        ],
    ])
]);
$pizza = json_decode($response->getBody(), true);

DELETE request

<?php

$client = $this->clientService->newClient();
$response = $client->delete('https://api.domain.tld/pizza/42');

OPTIONS request

<?php

$client = $this->clientService->newClient();
$response = $client->options('https://nextcloud.com');
$status = $response->getStatusCode();
$allHeaders = $response->getHeaders();
$contentType = $response->getHeader('content-type');

Error handling

Errors are signaled with exceptions. Catch PHP’s base Exception.

<?php

use Exception;

$client = $this->clientService->newClient();
try {
    $response = $client->options('https://nextcloud.com');
} catch (\Exception $e) {
    // Handle the error
}