修复phpcms自带采集无法采集https网站内容

无法采集https的网站内容主要是https不支持file_get_contents获取内容,所以可以考虑采用curl的方式获取。(需要开启curl,可以在pathinfo里边查看)

(1)打开phpcmsmodulescollectionclassescollection.class.php
在类里边添加新函数:
  1. protected static function curl_request($url){   
  2.         if (!function_exists('curl_init')) {   
  3.             throw new Exception('server not install curl');   
  4.         }   
  5.         $ch = curl_init(); 
  6.         curl_setopt($ch, CURLOPT_URL,$url); 
  7.         curl_setopt($ch, CURLOPT_HEADER,0); 
  8.         curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);//禁止调用时就输出获取到的数据 
  9.         curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1); 
  10.         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false); 
  11.         curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,false); 
  12.         $result = curl_exec($ch); 
  13.         curl_close($ch); 
  14.         return $result; 
  15.     }   

(2)找到函数function get_htm把该函数
  1. protected static function get_html($url, &$config) { 
  2.         if (!empty($url) && $html = @file_get_contents($url)) { 
  3.             if ($syscharset != $config['sourcecharset'] && $config['sourcetype'] != 4) { 
  4.                 $html = iconv($config['sourcecharset'], CHARSET.'//TRANSLIT//IGNORE', $html); 
  5.             } 
  6.             return $html; 
  7.         } else { 
  8.             return false
  9.         } 
  10.     } 
改成
  1. protected static function get_html($url, &$config) { 
  2.         if(substr(trim($url),0, 5) == "https"){
              $html = @self::curl_request($url);
          }else{
             $html = @file_get_contents($url);
           }
  3.         if (!empty($url) && $html) { 
  4.             if ($syscharset != $config['sourcecharset'] && $config['sourcetype'] != 4) { 
  5.                 $html = iconv($config['sourcecharset'], CHARSET.'//TRANSLIT//IGNORE', $html); 
  6.             } 
  7.             return $html; 
  8.         } else { 
  9.             return false
  10.         } 
  11.     } 
然后保存即可获取,测试结果



不知道是否还有其他bug,欢迎留言反馈!
本文关键词:

联系我们

在线咨询:点击这里给我发消息

邮件:w420220301@qq.com