PHP
Joomla! 6
Joomla! 5
Joomla! 3 (Legacy)
Helper / API
Intermediate
Make External HTTP Requests with HttpFactory::getHttp()
Consumes external REST APIs with timeouts, Bearer headers and status checks using Joomla native HTTP client.
components/com_example/src/Helper/ApiClient.php
<?php
declare(strict_types=1);
use Joomla\CMS\Http\HttpFactory;
$http = HttpFactory::getHttp();
$headers = [
'Accept' => 'application/json',
'Authorization' => 'Bearer ' . $apiToken,
];
$response = $http->get('https://api.example.com/v1/status', $headers, 10);
if ($response->code === 200) {
$data = json_decode((string) $response->body, true);
}
Always pass a strict timeout so slow third-party APIs never stall PHP-FPM workers.
components/com_example/src/Helper/ApiClient.php
<?php
$http = JHttpFactory::getHttp();
$response = $http->get('https://api.example.com/v1/status', array('Accept' => 'application/json'), 10);
$data = json_decode($response->body, true);
Legacy Joomla 3 version via JHttpFactory::getHttp().
Technical explanation and best practices
Avoid file_get_contents() on remote URLs. HttpFactory::getHttp() automatically selects cURL or stream transports with proper timeouts, TLS verification and custom headers.