A-A+

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

2022年12月02日 PHP开源系统 暂无评论 阅读 0 views 次

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

(1)打开phpcms\modules\collection\classes\collection.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 (!emptyempty($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"){  
  3.              $html = @self::curl_request($url);  
  4.         }else{  
  5.              $html = @file_get_contents($url);  
  6.         }  
  7.         if (!emptyempty($url) && $html) {   
  8.             if ($syscharset != $config['sourcecharset'] && $config['sourcetype'] != 4) {   
  9.                 $html = iconv($config['sourcecharset'], CHARSET.'//TRANSLIT//IGNORE', $html);   
  10.             }   
  11.             return $html;   
  12.         } else {   
  13.             return false;   
  14.         }   
  15.     }   

然后保存即可获取,测试结果:

不知道是否还有其他bug,欢迎留言反馈!

标签:

给我留言