HTTP request library

Since TYPO3 CMS 4.6, a library for easily making HTTP requests is available. It is actually a wrapper around the HTTP_Request2 PEAR package, which is shipped with the Core.

Basic usage

The basic usage is as simple as it gets:

$request = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(
$result = $request->send();
$content = $result->getBody();

The above example will read the content of the “” home page.


This example is taken from the “linkvalidator” system extension.

$config = array(
        'follow_redirects' => TRUE,
        'strict_redirects' => TRUE
/** @var \TYPO3\CMS\Core\Http\HttpRequest|\HTTP_Request2 $request */
$request = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(
// Observe cookies
try {
        /** @var \HTTP_Request2_Response $response */
        $response = $request->send();
        // HEAD was not allowed, now trying GET
        if (isset($response) && $response->getStatus() === 405) {
                $request->setHeader('Range', 'bytes = 0 - 4048');
                /** @var \HTTP_Request2_Response $response */
                $response = $request->send();
} catch (\Exception $e) {
        $isValidUrl = FALSE;
        // A redirect loop occurred
        if ($e->getCode() === 40) {
                // Parse the exception for more information
                $trace = $e->getTrace();
                $traceUrl = $trace[0]['args'][0]->getUrl()->getUrl();
                $traceCode = $trace[0]['args'][1]->getStatus();
                $errorParams['errorType'] = 'loop';
                $errorParams['location'] = $traceUrl;
                $errorParams['errorCode'] = $traceCode;
        } else {
                $errorParams['errorType'] = 'exception';
        $errorParams['message'] = $e->getMessage();

This is the code that checks external links. To keep the traffic low, it first checks only with HEAD. In case the server does not allow this, the check is retried with GET. In such a case, we get only the first 4 kilobytes, as we don’t really care about the content itself.

It may happen that the check gets stuck in a redirect loop, in which case an exception is thrown. This particular case it tested for in the catch block above to better inform the user about what happened.