迅睿PHPcms-火车头内容采集范例,支持下载远程图片,提取第一张做缩略图

文档创建者:admin
浏览次数:47
最后更新:2026-04-12
Help Center
Help Center: 帮助中心
车头内容采集范例,支持下载远程图片,提取第一张做缩略图,支持入库推荐位
官方教程《火车头内容采集范例》 https://www.xunruicms.com/doc/1084.html
把官方教程中的 dayrui/My/Api/Caiji.php 整个改成
  1. <?php
  2. $this->_module_init('news'); // news 是模块目录

  3. if ($_GET['action'] == 'category')
  4. {
  5.     // 显示栏目
  6.     if (!$this->module['category'])
  7.     {
  8.         echo '模块【' . $this->module['dirname'] . '】没有创建栏目';
  9.     }
  10.     foreach ($this->module['category'] as $t)
  11.     {
  12.         if ($t['child'] == 0 && $t['tid'] == 1)
  13.         {
  14.             echo '<h1>' . $t['name'] . '<=>' . $t['id'] . '</h1>' . PHP_EOL;
  15.         }
  16.     }

  17. }
  18. else
  19. {
  20.     // 入库数据

  21.     $data = $_REQUEST;

  22.     // 发布者id 1
  23.     $data['uid'] = 1;

  24.     // 发布者账号 admin
  25.     $data['author'] = 'admin';

  26.     // 主表字段
  27.     $fields[1] = $this->get_cache('table-' . SITE_ID, $this->content_model->dbprefix(SITE_ID . '_' . MOD_DIR));
  28.     $cache     = $this->get_cache('table-' . SITE_ID, $this->content_model->dbprefix(SITE_ID . '_' . MOD_DIR . '_category_data'));
  29.     $cache && $fields[1] = array_merge($fields[1], $cache);

  30.     // 附表字段
  31.     $fields[0] = $this->get_cache('table-' . SITE_ID, $this->content_model->dbprefix(SITE_ID . '_' . MOD_DIR . '_data_0'));

  32.     // 去重复
  33.     $fields[0] = array_unique($fields[0]);
  34.     $fields[1] = array_unique($fields[1]);

  35.     // 格式化入库字段

  36.     // 一般是格式化非文本类的字段(例如多文件上传、复选框、联动字段等等)
  37.     // 这里需要按采集资料的格式入库格式化字段,


  38.     // 开始归类存储

  39.     $save = [];

  40.     // 主表附表归类
  41.     foreach ($fields as $ismain => $field)
  42.     {
  43.         foreach ($field as $name)
  44.         {
  45.             isset($data[$name]) && $save[$ismain][$name] = $data[$name];
  46.         }
  47.     }
  48.     if (!$data['catid'])
  49.     {
  50.         exit('栏目为空');
  51.     }

  52.     $save[1]['uid']   = $save[0]['uid'] = $data['uid'];
  53.     $save[1]['catid'] = $save[0]['catid'] = $data['catid'];

  54.     $save[1]['url']          = '';
  55.     $save[1]['status']       = 9; //9表示正常发布,1表示审核里面
  56.     $save[1]['hits']         = 0;
  57.     $save[1]['displayorder'] = 0;
  58.     $save[1]['link_id']      = 0;
  59.     $save[1]['inputtime']    = $save[1]['updatetime'] = SYS_TIME + rand(0, 7200);
  60.     $save[1]['inputip']      = '127.0.0.1';

  61.     //$save[1]['keywords'] = dr_get_keywords( $save[1]['title']); // 按插件提取关键词

  62.     //$save[1]['description'] = dr_get_description( $save[0]['content'], 100); // 在内容里面提取100个子作为描述

  63.     // 验证标题重复
  64.     if ($this->content_model->table(SITE_ID . '_' . MOD_DIR)->where('title', $save[1]['title'])->counts())
  65.     {
  66.         echo '重复';
  67.         exit;
  68.     }

  69.     // 文档内容
  70.     $value = $data['content'];
  71.     // 第一张作为缩略图
  72.     $slt = 1;
  73.     // 是否下载图片
  74.     $yct = 1;
  75.     $base64 = strpos($value, ';base64,');
  76.     // 附件入库标记字符
  77.     $rid = md5(FC_NOW_URL.\Phpcmf\Service::L('input')->get_user_agent().\Phpcmf\Service::L('input')->ip_address().\Phpcmf\Service::C()->uid);
  78.     // 下载远程图片
  79.     if ($yct || $slt || $base64) {
  80.         $temp = preg_replace('/<pre(.*)<\/pre>/siU', '', $value);
  81.         $temp = preg_replace('/<code(.*)<\/code>/siU', '', $temp);
  82.         if (preg_match_all("/(src)=(["|']?)([^ "'>]+)\\2/i", $temp, $imgs)) {
  83.             foreach ($imgs[3] as $img) {

  84.                 if ($base64 && preg_match('/^(data:\s*image\/(\w+);base64,)/i', $img, $result)) {
  85.                     // 处理图片
  86.                     $ext = strtolower($result[2]);
  87.                     if (!in_array($ext, ['png', 'jpg', 'jpeg', 'gif', 'webp'])) {
  88.                         continue;
  89.                     }
  90.                     $content = base64_decode(str_replace($result[1], '', $img));
  91.                     if (strlen($content) > 30000000) {
  92.                         continue;
  93.                     }
  94.                     $rt = \Phpcmf\Service::L('upload')->base64_image([
  95.                         'ext' => $ext,
  96.                         'content' => $content,
  97.                         'watermark' => \Phpcmf\Service::C()->get_cache('site', SITE_ID, 'watermark', 'ueditor') || $field['setting']['option']['watermark'] ? 1 : 0,
  98.                         'attachment' => \Phpcmf\Service::M('Attachment')->get_attach_info(intval($field['setting']['option']['attachment']), $field['setting']['option']['image_reduce']),
  99.                     ]);
  100.                     if (!$rt['code']) {
  101.                         continue;
  102.                     }
  103.                     $att = \Phpcmf\Service::M('Attachment')->save_data($rt['data'], 'ueditor:'.$rid);
  104.                     if ($att['code']) {
  105.                         // 归档成功
  106.                         $value = str_replace($img, $rt['data']['url'], $value);
  107.                         $img = $att['code'];
  108.                         // 标记附件
  109.                         \Phpcmf\Service::M('Attachment')->save_ueditor_aid($rid, $att['code']);
  110.                     }
  111.                 } else {
  112.                     $ext = _get_image_ext($img);
  113.                     if (!$ext) {
  114.                         continue;
  115.                     }
  116.                     // 下载图片
  117.                     if ($yct && strpos($img, 'http') === 0) {
  118.                         if (dr_is_app('mfile') && \Phpcmf\Service::M('mfile', 'mfile')->check_upload(\Phpcmf\Service::C()->uid)) {
  119.                             //用户存储空间已满
  120.                         } else {
  121.                             // 正常下载
  122.                             // 判断域名白名单
  123.                             $arr = parse_url($img);
  124.                             $domain = $arr['host'];
  125.                             if ($domain) {
  126.                                 $sites = \Phpcmf\Service::R(WRITEPATH.'config/domain_site.php');
  127.                                 if (isset($sites[$domain])) {
  128.                                     // 过滤站点域名
  129.                                 } elseif (strpos(SYS_UPLOAD_URL, $domain) !== false) {
  130.                                     // 过滤附件白名单
  131.                                 } else {
  132.                                     $zj = 0;
  133.                                     $remote = \Phpcmf\Service::C()->get_cache('attachment');
  134.                                     if ($remote) {
  135.                                         foreach ($remote as $t) {
  136.                                             if (strpos($t['url'], $domain) !== false) {
  137.                                                 $zj = 1;
  138.                                                 break;
  139.                                             }
  140.                                         }
  141.                                     }
  142.                                     if ($zj == 0) {
  143.                                         // 可以下载文件
  144.                                         // 下载远程文件
  145.                                         $rt = \Phpcmf\Service::L('upload')->down_file([
  146.                                             'url' => $img,
  147.                                             'timeout' => 5,
  148.                                             'watermark' => \Phpcmf\Service::C()->get_cache('site', SITE_ID, 'watermark', 'ueditor') || $field['setting']['option']['watermark'] ? 1 : 0,
  149.                                             'attachment' => \Phpcmf\Service::M('Attachment')->get_attach_info(intval($field['setting']['option']['attachment']), $field['setting']['option']['image_reduce']),
  150.                                             'file_ext' => $ext,
  151.                                         ]);
  152.                                         if ($rt['code']) {
  153.                                             $att = \Phpcmf\Service::M('Attachment')->save_data($rt['data'], 'ueditor:'.$rid);
  154.                                             if ($att['code']) {
  155.                                                 // 归档成功
  156.                                                 $value = str_replace($img, $rt['data']['url'], $value);
  157.                                                 $img = $att['code'];
  158.                                                 // 标记附件
  159.                                                 \Phpcmf\Service::M('Attachment')->save_ueditor_aid($rid, $att['code']);
  160.                                             }
  161.                                         }
  162.                                     }
  163.                                 }
  164.                             }
  165.                         }
  166.                     }
  167.                 }
  168.                 // 缩略图
  169.                 if ($img && $slt) {
  170.                     $_field = \Phpcmf\Service::L('form')->fields;
  171.                     if (isset($_field['thumb']) && $_field['thumb']['fieldtype'] == 'File' && !\Phpcmf\Service::L('Field')->data[$_field['thumb']['ismain']]['thumb']) {
  172.                         if (!is_numeric($img)) {
  173.                             // 下载缩略图
  174.                             // 判断域名白名单
  175.                             $arr = parse_url($img);
  176.                             $domain = $arr['host'];
  177.                             if ($domain) {
  178.                                 $file = dr_catcher_data($img, 8);
  179.                                 if (!$file) {
  180.                                     CI_DEBUG && log_message('debug', '服务器无法下载图片:'.$img);
  181.                                 } else {
  182.                                     // 尝试找一找附件库
  183.                                     $att = \Phpcmf\Service::M()->table('attachment')->like('related', 'ueditor')->where('filemd5', md5($file))->getRow();
  184.                                     if ($att) {
  185.                                         $img = $att['id'];
  186.                                     } else {
  187.                                         // 下载归档
  188.                                         $rt = \Phpcmf\Service::L('upload')->down_file([
  189.                                             'url' => $img,
  190.                                             'timeout' => 5,
  191.                                             'watermark' => \Phpcmf\Service::C()->get_cache('site', SITE_ID, 'watermark', 'ueditor') || $field['setting']['option']['watermark'] ? 1 : 0,
  192.                                             'attachment' => \Phpcmf\Service::M('Attachment')->get_attach_info(intval($field['setting']['option']['attachment']), $field['setting']['option']['image_reduce']),
  193.                                             'file_ext' => $ext,
  194.                                             'file_content' => $file,
  195.                                         ]);
  196.                                         if ($rt['code']) {
  197.                                             $att = \Phpcmf\Service::M('Attachment')->save_data($rt['data'], 'ueditor:'.$rid);
  198.                                             if ($att['code']) {
  199.                                                 // 归档成功
  200.                                                 $value = str_replace($img, $rt['data']['url'], $value);
  201.                                                 $img = $att['code'];
  202.                                                 // 标记附件
  203.                                                 \Phpcmf\Service::M('Attachment')->save_ueditor_aid($rid, $att['code']);
  204.                                             }
  205.                                         }
  206.                                     }
  207.                                 }
  208.                             }
  209.                         }
  210.                         \Phpcmf\Service::L('Field')->data[$_field['thumb']['ismain']]['thumb'] = $_POST['data']['thumb'] = $img;
  211.                     }
  212.                 }
  213.             }
  214.         }
  215.     }

  216.     // 最后得到的文档内容
  217.     $save[0]['content'] = $value;
  218.     // 最后得到的缩略图
  219.     $save[1]['thumb'] = $img;

  220.     $rt = $this->content_model->save_content(0, $save);

  221.     if ($rt['code'])
  222.     {
  223.         /*
  224.         // 用于发布成功后生成静态文件代码
  225.         //dr_html_auth($_SERVER['SERVER_ADDR']);
  226.         //dr_catcher_data(SITE_URL.'index.php?s='.MOD_DIR.'&c=html&m=showfile&id='.$rt['id']);
  227.         $atcode = 'chtml_'.SITE_ID.'_'.MOD_DIR.'_'.$rt['code'];
  228.         \Phpcmf\Service::L('cache')->set_auth_data($atcode, $rt['code'], SITE_ID);
  229.         dr_catcher_data(SITE_URL.'index.php?s='.MOD_DIR.'&c=html&m=showfile&id='.$rt['code'].'&atcode='.$atcode);
  230.         */
  231.         // 处理推荐位
  232.         if ($data['flag']) {
  233.             $myflag = explode(',', $data['flag']);
  234.             foreach ($myflag as $i) {
  235.                 if (isset($this->module['setting']['flag'][$i])) {
  236.                     $this->content_model->insert_flag((int)$i, $rt['code'], $rt['data'][1]['uid'], $rt['data'][1]['catid']);
  237.                 }
  238.             }
  239.         }
  240.         exit('成功');
  241.     }
  242.     else
  243.     {
  244.         exit('失败');
  245.     }

  246. }

  247. // 获取远程附件扩展名
  248. function _get_image_ext($url) {

  249.     if (strlen($url) > 300) {
  250.         return '';
  251.     }

  252.     $arr = ['gif', 'jpg', 'jpeg', 'png', 'webp'];
  253.     $ext = str_replace('.', '', trim(strtolower(strrchr($url, '.')), '.'));
  254.     if ($ext && in_array($ext, $arr)) {
  255.         return $ext; // 满足扩展名
  256.     } elseif ($ext && strlen($ext) < 4) {
  257.         //CI_DEBUG && log_message('error', '此路径不是远程图片:'.$url);
  258.         return ''; // 表示不是图片扩展名了
  259.     }

  260.     foreach ($arr as $t) {
  261.         if (stripos($url, $t) !== false) {
  262.             return $t;
  263.         }
  264.     }

  265.     $rt = getimagesize($url);
  266.     if ($rt && $rt['mime']) {
  267.         foreach ($arr as $t) {
  268.             if (stripos($rt['mime'], $t) !== false) {
  269.                 return $t;
  270.             }
  271.         }
  272.     }

  273.     CI_DEBUG && log_message('debug', '服务器无法获取远程图片的扩展名:'.dr_safe_replace($url));

  274.     return '';
  275. }

  276. exit;
复制代码




您需要登录后才可以回帖 登录 | 立即注册

本版积分规则