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

时间:2013-04-05 04:48:58

标签: php

目前我正致力于构建一个支持SSL的SMF模式,并且一个不断涌现的问题是用户嵌入内容(如图像)以及其他未通过SSL的网站提供的内容。我正在构建一种在我的网站上运行的代理脚本的方法,当传递URL时,将检索文件,维护大部分标题(例如,mime类型),然后再次提供文件。通过这种方式,它看起来像是从启用SSL的站点实际提供的图像或其他任何内容,而实际上它不是。

1 个答案:

答案 0 :(得分:1)

感谢所有评论!对于那些希望将来做这件事的人来说,正是我正在使用的剧本:

<?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'];
        }
    }

?>