Attention

TYPO3 v6 has reached its end-of-life April 18th, 2017 and is not maintained by the community anymore. Looking for a stable version? Use the version switch on the top left.

There is no further ELTS support. It is strongly recommended updating your project.

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(
        'TYPO3\\CMS\\Core\\Http\\HttpRequest',
        'http://typo3.org/'
);
$result = $request->send();
$content = $result->getBody();

The above example will read the content of the "typo3.org" home page.

Example

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(
        'TYPO3\\CMS\\Core\\Http\\HttpRequest',
        $url,
        'HEAD',
        $config
);
// Observe cookies
$request->setCookieJar(TRUE);
try {
        /** @var \HTTP_Request2_Response $response */
        $response = $request->send();
        // HEAD was not allowed, now trying GET
        if (isset($response) && $response->getStatus() === 405) {
                $request->setMethod('GET');
                $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.