Introduction
Requests to other services over HTTP
Fegg\Http\Client is a small HTTP client for calling other services: an API, a webhook, a feed. Fegg itself uses it for the captcha checks, RSS and fegg-cli doctor.
- Every request has a timeout (10 seconds for the whole request and 3 to connect, by default).
- Only
http://andhttps://URLs are requested. - Redirects are not followed: a
301or302comes back as it is. - It uses cURL when the extension is installed, otherwise PHP's http stream wrapper. Nothing needs to be configured.
How to use
Import Fegg\Http\Client, create it with new and call get(), post() or request(). Each returns an array:
| Key | Data type | Remarks |
|---|---|---|
| status | int | HTTP status (200, 404 ...) |
| body | string | Response body |
Controller
<?php
namespace Site\Controller;
defined('FEGG_CORE_DIR') || exit;
use Fegg\Http\Client;
use Fegg\Http\ClientException;
use Fegg\Log;
class Items extends Controller
{
public function index()
{
$items = array();
try {
$response = (new Client(5))->get('https://api.example.com/items?category=2', array('Accept: application/json'));
if ($response['status'] === 200) {
$items = json_decode($response['body'], true) ?: array();
} else {
Log::warning('Items API answered {status}', array('status' => $response['status']));
}
} catch (ClientException $e) {
Log::warning('Items API not reachable: {message}', array('message' => $e->getMessage()));
}
$this->page['items'] = $items;
$this->displayPage('items');
}
}
Errors
- A response with an error status (
404,500...) is returned normally: checkstatus. - Only a transport failure (no connection, DNS, TLS, timeout) throws
Fegg\Http\ClientException(a\RuntimeException). The message names the method, the URL and the cause. - A URL that is not
httporhttpsthrows\InvalidArgumentExceptionbefore anything is sent.
The page should still work when the other service does not: catch ClientException, write it to the log and show the page without that part. Keep the timeout short for a call made while a visitor waits.
Methods
Client::__construct
Create a client with its timeouts
Client::get
Send a GET request
Client::post
Send a POST request
Client::request
Send a request with any method