聯(lián)系官方銷售客服
1835022288
028-61286886
采集文章,圖片下載入庫,同樣的代碼以前可以,現(xiàn)在不行了。
要采集的網(wǎng)址是 www.b.com, 這個(gè)b.com 中的新聞內(nèi)容的中的圖片 的網(wǎng)址是 http://6061627.s21i.faiusr.com/4/A.png 這樣 格式的。
采集到本地后,內(nèi)容的圖片還是上面的這個(gè)地址, 無法圖片下載入庫本地。誰給看就看啥問題
采集代碼中 下載圖片如下:
<?php
$this->_module_init('news'); // news 是模塊目錄
if ($_GET['action'] == 'category') {
// 顯示欄目
foreach ($this->module['category'] as $t) {
if ($t['child'] == 0 && $t['tid'] == 1) {
echo '<h1>'.$t['name'].'<=>'.$t['id'].'</h1>'.PHP_EOL;
}
}
} else {
// 入庫數(shù)據(jù)
$data = $_REQUEST;
// 發(fā)布者id 1
$data['uid'] = 1;
// 發(fā)布者賬號(hào) admin
$data['author'] = 'admin';
// 主表字段
$fields[1] = $this->get_cache('table-'.SITE_ID, $this->content_model->dbprefix(SITE_ID.'_'.MOD_DIR));
$cache = $this->get_cache('table-'.SITE_ID, $this->content_model->dbprefix(SITE_ID.'_'.MOD_DIR.'_category_data'));
$cache && $fields[1] = array_merge($fields[1], $cache);
// 附表字段
$fields[0] = $this->get_cache('table-'.SITE_ID, $this->content_model->dbprefix(SITE_ID.'_'.MOD_DIR.'_data_0'));
$cache = $this->get_cache('table-'.SITE_ID, $this->content_model->dbprefix(SITE_ID.'_'.MOD_DIR.'_category_data_0'));
$cache && $fields[0] = array_merge($fields[0], $cache);
// 去重復(fù)
$fields[0] = array_unique($fields[0]);
$fields[1] = array_unique($fields[1]);
$save = [];
// 主表附表歸類
foreach ($fields as $ismain => $field) {
foreach ($field as $name) {
isset($data[$name]) && $save[$ismain][$name] = $data[$name];
}
}
if (!$data['catid']) {
exit('欄目為空');
}
$save[1]['uid'] = $save[0]['uid'] = $data['uid'];
$save[1]['catid'] = $save[0]['catid'] = $data['catid'];
$value = $save[0]['content'];
if (preg_match_all("/(src)=([\"|']?)([^ \"'>]+\.(gif|jpg|jpeg|png))\\2/i", $value, $imgs)) {
foreach ($imgs[3] as $img) {
if (strpos($img, '/api/ueditor/') !== false
|| strpos($img, '/api/umeditor/') !== false) {
continue;
}
// 下載圖片
if (strpos($img, 'http') === 0) {
// 正常下載
// 判斷域名白名單
$arr = parse_url($img);
$domain = $arr['host'];
if ($domain) {
$sites = WRITEPATH.'config/domain_site.php';
if (isset($sites[$domain])) {
// 過濾站點(diǎn)域名
} elseif (strpos(SYS_UPLOAD_URL, $domain) !== false) {
// 過濾附件白名單
} else {
$zj = 0;
$remote = \Phpcmf\Service::C()->get_cache('attachment');
if ($remote) {
foreach ($remote as $t) {
if (strpos($t['url'], $domain) !== false) {
$zj = 1;
break;
}
}
}
if ($zj == 0) {
// 可以下載文件
// 同步模式
// 下載遠(yuǎn)程文件
$rt = \Phpcmf\Service::L('upload')->down_file([
'url' => $img,
'attachment' => \Phpcmf\Service::M('Attachment')->get_attach_info(intval($field['content']['setting']['option']['attachment'])),
]);
if ($rt['code']) {
$att = \Phpcmf\Service::M('Attachment')->save_data($rt['data'], 'ueditor_down_img');
if ($att['code']) {
// 歸檔成功
$value = str_replace($img, $rt['data']['url'], $value);
$img = $att['code'];
}
}
}
}
}
}
// 縮略圖
if ($img && !$save[1]['thumb']) {
$save[1]['thumb'] = $img;
}
}
}
if ($imgs && !$save[1]['thumb']) {
$save[1]['thumb'] = $imgs[3][0];
}
$save[0]['content'] = $value;
$save[1]['url'] = '';
$save[1]['status'] = 9; //9表示正常發(fā)布,1表示審核里面
$save[1]['hits'] = $data['hits'];
$save[1]['displayorder'] = 0;
$save[1]['link_id'] = 0;
$save[1]['inputtime'] = $save[1]['updatetime'] = strtotime($data['inputtime']); // 更新時(shí)間
$save[1]['inputip'] = '127.0.0.1';
// 驗(yàn)證標(biāo)題重復(fù)
if ($this->content_model->table(SITE_ID.'_'.MOD_DIR)->where('title', $save[1]['title'])->counts()) {
echo '重復(fù)';exit;
}
$rt = $this->content_model->save_content(0, $save);
if ($rt['code']) {
exit('成功');
} else {
exit('失敗');
}
}
exit;
以我的經(jīng)驗(yàn),我建議你這樣做
在采集軟件里面把圖片直接下載,然后在采集軟件里面替換圖片路徑,火車頭最新版就支持這個(gè)替換功能,這樣效率才最高
回復(fù)@官方技術(shù)總監(jiān)-陳總
好的,,馬上測(cè)試一下,多謝
我覺得陳總的方法可取,在火車頭里面采集下載好,畢竟在php里面下載效率并不高的
建議找小波工作室,他比較擅長(zhǎng)采集入庫 ?? ??