dedecms5.7 sp1版本远程图片遇到Https无法本地化解决办法

本文介绍了解决DEDECMS系统中HTTPS远程图片无法本地化的问题,通过修改源代码实现对HTTPS的支持,确保图片资源能被正确抓取及存储。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文为大家介绍一下DEDECMS远程图片遇到Https无法本地化解决办法,由于现在许多网站使用了HTTPS技术,所以我们在DEDE后台发布文章时,可能会遇到对于https的就无法本地化的问题,关于http的远程图片本地化,要想支持HTTPS,其实也非常简单,以下是解决办法:

第一步 在include 文件夹里面的dedehttpdown.class.php文件中有个PrivateStartSession函数,将下面的代码放入函数的开头

if ($this->m_scheme == "https") {
            $this->m_port = "443";
        }
        if (function_exists('curl_init') && function_exists('curl_exec')) {
            $this->m_ch = curl_init();
            curl_setopt($this->m_ch, CURLOPT_URL, $this->m_scheme.'://'.$this->m_host.':'.$this->m_port.$this->m_path);
            curl_setopt($this->m_ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($this->m_ch, CURLOPT_FOLLOWLOCATION, 1);
            if ($requestType == "POST") {
                curl_setopt($this->m_ch, CURLOPT_POST, 1);
                // $content = is_array($post) ? http_build_query($post) : $post;
                // curl_setopt($this->m_ch, CURLOPT_POSTFIELDS, urldecode($content));
            }
            if (!empty($this->m_cookies)) {
                curl_setopt($this->m_ch, CURLOPT_COOKIE, $this->m_cookies);
            }
            if ($this->m_scheme == "https") {
                curl_setopt($this->m_ch, CURLOPT_SSL_VERIFYPEER, false);
                curl_setopt($this->m_ch, CURLOPT_SSL_VERIFYHOST, false);
            }

            $this->m_puthead = array();
            $this->m_puthead["Host"] = $this->m_host;

            //发送用户自定义的请求头
            if(!isset($this->m_puthead["Accept"]))
            {
                $this->m_puthead["Accept"] = "*/*";
            }
            if(!isset($this->m_puthead["User-Agent"]))
            {
                $this->m_puthead["User-Agent"] = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2)";
            }
            if(!isset($this->m_puthead["Refer"]))
            {
                $this->m_puthead["Refer"] = "http://".$this->m_puthead["Host"];
            }
            $headers = array();
            foreach($this->m_puthead as $k=>$v)
            {
                $k = trim($k);
                $v = trim($v);
                if($k!=""&&$v!="")
                {
                    $headers[] = "$k: $v";
                }
            }
            if (count($headers) > 0) {
                curl_setopt($this->m_ch, CURLOPT_HTTPHEADER, $headers);
            }
            
            curl_setopt($this->m_ch, CURLOPT_CONNECTTIMEOUT, 20);
            curl_setopt($this->m_ch, CURLOPT_TIMEOUT, 900);

            $this->m_html = curl_exec($this->m_ch);
            $status = curl_getinfo($this->m_ch);
            if (count($status) > 0) {
                foreach ($status as $key => $value) {
                    $key = str_replace("_", "-", $key);
                    if ($key == "http-code") {
                        $this->m_httphead["http-state"] = $value;
                    }
                    $this->m_httphead[$key] = $value;
                }
            }
            $this->m_error = curl_errno($this->m_ch);

            return TRUE;
        }

第二步

    还有找到一个SaveToBin函数
    在


 if(!$this->IsGetOK())
    {
        return FALSE;
    }
    
    if(@feof($this->m_fp))
    {
        $this->m_error = "连接已经关闭!"; return FALSE;
    }

    这两个if中间在加上个if判断


 if (function_exists('curl_init') && function_exists('curl_exec')) {
        file_put_contents($savefilename, $this->m_html);
        return TRUE;
    }

第三步
找到dede/inc/inc_archives_functions.php 定位到文件里面GetCurContent($body)这个函数
将一下代码:

reg_match_all("/src=[\"|’|\s]{0,}(http:\/\/([^>]*)\.(gif|jpg|png|jpeg|bmp))/isU",$body,$img_array);
$img_array = array_unique($img_array[1])

改为:

preg_match_all("/src=[\"|’|\s]{0,}(http:\/\/([^>]*)\.(gif|jpg|png|jpeg|bmp))/isU",$body,$img_array);
preg_match_all("/src=[\"|’|\s]{0,}(https:\/\/([^>]*)\.(gif|jpg|png|jpeg|bmp))/isU",$body,$img_array_https);
$img_array = array_unique($img_array[1]); 
$img_array_https = array_unique($img_array_https[1]); 
$img_array=array_merge_recursive($img_array,$img_array_https);

第四步
找到:

if(!preg_match("#^http:\/\/#i", $value))
{
continue;
}

改为:

if(!preg_match("#^http:\/\/#i", $value)&&!preg_match("#^https:\/\/#i", $value))
{
continue;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值