Cliente HTTP¶
Resumen¶
Esta página explica cómo usar el cliente HTTP integrado para enviar solicitudes a otros servidores web: cómo obtenerlo con la factoría IClientService, un ejemplo de cada tipo de solicitud y cómo manejar los errores. Está dirigida a quienes desarrollan apps.
Fuente: documentación de Nextcloud, «developer_manual/digging_deeper/http_client», versión 3ad9158, licencia CC BY 3.0; traducida y adaptada para APS Conecta Gestión.
APS Conecta Gestión incluye un cliente HTTP sencillo que puede usarse para enviar solicitudes a otros servidores web. Este cliente respeta la configuración, las restricciones de seguridad y los ajustes de proxy de APS Conecta Gestión.
Obtener un cliente HTTP¶
Las instancias del cliente HTTP se construyen con la factoría del servicio de cliente, IClientService. La factoría puede inyectarse en cualquier clase de la app:
<?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();
}
}
Solicitud HEAD¶
<?php
$client = $this->clientService->newClient();
$response = $client->head('https://nextcloud.com');
$body = $response->getBody();
Solicitud GET¶
<?php
$client = $this->clientService->newClient();
$response = $client->get('https://nextcloud.com');
$body = $response->getBody();
Solicitud POST¶
<?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);
Solicitud PUT¶
<?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);
Solicitud DELETE¶
<?php
$client = $this->clientService->newClient();
$response = $client->delete('https://api.domain.tld/pizza/42');
Solicitud OPTIONS¶
<?php
$client = $this->clientService->newClient();
$response = $client->options('https://nextcloud.com');
$status = $response->getStatusCode();
$allHeaders = $response->getHeaders();
$contentType = $response->getHeader('content-type');
Manejo de errores¶
Los errores se señalan con excepciones. Capturar la Exception base de PHP.
<?php
use Exception;
$client = $this->clientService->newClient();
try {
$response = $client->options('https://nextcloud.com');
} catch (\Exception $e) {
// Handle the error
}