从URL检索文件,并使用相同的头文件提供给它


Retrieve a file from a URL and serve it with the same headers

目前,我正在构建一个启用SSL的SMF模块,不断出现的一件事是用户嵌入内容(如图像)和其他类型的东西不能从SSL站点提供的问题。我是在一种方式来建立一种代理脚本,将在我的网站上运行,当传递一个URL,将检索文件,维护大部分的头(如。Mime类型),然后再次提供该文件。这样看起来图像或其他东西实际上是从启用SSL的站点提供的,而实际上不是。

感谢大家的评论!对于那些将来想要这样做的人,下面是我使用的脚本:

<?php
    $requestURL = filter_var($_REQUEST["url"], FILTER_SANITIZE_URL);
    if(filter_var($requestURL, FILTER_VALIDATE_URL, FILTER_FLAG_PATH_REQUIRED))
    {
        if(isset($_SERVER['HTTP_IF_MODIFIED_SINCE']))
        {
            // Load the headers from the destination server.
            $uri_info = new URIInfo($requestURL);
            if($uri_info->info['filetime'] == strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']))
            {
                header('HTTP/1.1 304 Not Modified');
            }
            else
            {
                //Serve a new copy of the file, its changed :O
                $ch = curl_init();
                curl_setopt($ch, CURLOPT_URL, $requestURL);
                curl_setopt($ch, CURLOPT_HEADER, 0);
                curl_setopt($ch, CURLOPT_FILETIME, 1);
                curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
                $data = curl_exec($ch);
                $info = curl_getinfo($ch);
                curl_close($ch);
                //Display the image in the browser
                header('Content-type: ' . $info['content_type']);
                header('Last-Modified: ' . gmdate('D, d M Y H:i:s 'G'M'T', $info['filetime']));
                header('Cache-Control: no-cache'); //Stop CloudFlare!
                echo $data;
            }
        }
        else
        {
            //Just serve the file, they obviously don't have a copy of it!
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $requestURL);
            curl_setopt($ch, CURLOPT_HEADER, 0);
            curl_setopt($ch, CURLOPT_FILETIME, 1);
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
            $data = curl_exec($ch);
            $info = curl_getinfo($ch);
            curl_close($ch);
            //Display the image in the browser
            header('Content-type: ' . $info['content_type']);
            header('Last-Modified: ' . gmdate('D, d M Y H:i:s 'G'M'T', $info['filetime']));
            header('Cache-Control: no-cache'); //Stop CloudFlare!
            echo $data;
        }
    }    
    exit();
?>


<?php
    class URIInfo 
    {
        public $info;
        public $header;
        private $url;
        public function __construct($url)
        {
            $this->url = $url;
            $this->setData();
        }
        public function setData() 
        {
            $curl = curl_init();
            curl_setopt($curl, CURLOPT_URL, $this->url);
            curl_setopt($curl, CURLOPT_FILETIME, 1);
            curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
            curl_setopt($curl, CURLOPT_NOBODY, 1);
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
            $this->header = curl_exec($curl);
            $this->info = curl_getinfo($curl);
            curl_close($curl);
        }
        public function getFiletime() 
        {
            return $this->info['filetime'];
        }
    }
?>