+ return array($headers, $content);
+}
+
+/**
+ * Retrieve HTTP headers, following n redirections (temporary and permanent ones).
+ *
+ * @param string $url initial URL to reach.
+ * @param int $redirectionLimit max redirection follow.
+ *
+ * @return array HTTP headers, or false if it failed.
+ */
+function get_redirected_headers($url, $redirectionLimit = 3)
+{
+ $headers = get_headers($url, 1);
+ if (!empty($headers['location']) && empty($headers['Location'])) {
+ $headers['Location'] = $headers['location'];
+ }
+
+ // Headers found, redirection found, and limit not reached.
+ if ($redirectionLimit-- > 0
+ && !empty($headers)
+ && (strpos($headers[0], '301') !== false || strpos($headers[0], '302') !== false)
+ && !empty($headers['Location'])) {
+
+ $redirection = is_array($headers['Location']) ? end($headers['Location']) : $headers['Location'];
+ if ($redirection != $url) {
+ $redirection = getAbsoluteUrl($url, $redirection);
+ return get_redirected_headers($redirection, $redirectionLimit);
+ }
+ }
+
+ return array($headers, $url);
+}
+
+/**
+ * Get an absolute URL from a complete one, and another absolute/relative URL.
+ *
+ * @param string $originalUrl The original complete URL.
+ * @param string $newUrl The new one, absolute or relative.
+ *
+ * @return string Final URL:
+ * - $newUrl if it was already an absolute URL.
+ * - if it was relative, absolute URL from $originalUrl path.
+ */
+function getAbsoluteUrl($originalUrl, $newUrl)
+{
+ $newScheme = parse_url($newUrl, PHP_URL_SCHEME);
+ // Already an absolute URL.
+ if (!empty($newScheme)) {
+ return $newUrl;