本文为大家介绍一下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;
}