织梦 PHP 实现织梦发布功能

创建一个本地数据库

CREATE TABLE IF NOT EXISTS `tb_demo` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '//ID',
  `title` varchar(250) NOT NULL COMMENT '//标题',
  `content` text NOT NULL COMMENT '//内容',
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

添加数据后的效果图:

 

index.php

<!DOCTYPE html>
<html lang="zh-cn">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
<title>发布织梦管理器</title>
<link rel="stylesheet" href="css/base.css" />
<style>
.box{ width:320px; border:1px solid #eee; margin:20px auto 0 auto; }
.box h1{ width:300px; height:48px; font-size:18px; line-height:48px; border-bottom:1px solid #eee; margin:0 auto; text-align:center; font-weight:bold; color:#f60; margin-bottom: 10px;}
.box p{ width: 300px; margin:0 auto; }
.box h3{ line-height:40px; text-align:center;}
.box h3 a{ color:#06f; font-weight:normal; text-decoration:underline; font-size:15px;}
.box h3 a:hover{ font-weight:bold;}
</style>
</head>
<body>
<div class="box">
	<h1>发布织梦管理器</h1>
	<p><a href="setJSON.php" target="_blank">发布数据到织梦后台里</a></p>
	<p><a href="cleanData.php" target="_blank">清理长度过短的数据</a></p>
</div>
</body>
</html>

 效果图:

 

cleanData.php

<?php
require "connect.inc.php";
ini_set('max_execution_time',200);

/******** 配置信息 BEGIN *******/
//Mysql的IP
$dbhost = '127.0.0.1';
//Mysql的账号
$dbuser = 'root';
//Mysql的密码
$dbpwd = '123456';
//Mysql里的数据库
$dbname = 'huocheDB';
//Mysql里的数据库指定那个表
$dbtable = 'tb_demo';


//标题长度,默认12个字节,三个字节等于一个汉字,所以是四个汉字,PS:一个字节等于一个字母
//$titlen = 12;
$titlen = 12;
//标题长度,默认120个字节,三个字节等于一个汉字,所以是四十个汉字,PS:一个字节等于一个字母
//$contlen = 120;
$contlen = 120;

/******** 配置信息 END *******/
?>

<!DOCTYPE html>
<html lang="zh-cn">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
<title>清理长度过短的数据</title>
<link rel="stylesheet" href="css/base.css" />
<style>
.box{ width:320px; border:1px solid #eee; margin:20px auto 0 auto; }
.box h1{ width:300px; height:48px; font-size:18px; line-height:48px; border-bottom:1px solid #eee; margin:0 auto; text-align:center; font-weight:bold; color:#f60; margin-bottom: 10px;}
.box p{ width: 300px; margin:0 auto; }
.box h3{ line-height:40px; text-align:center;}
.box h3 a{ color:#06f; font-weight:normal; text-decoration:underline; font-size:15px;}
.box h3 a:hover{ font-weight:bold;}
</style>
</head>
<body>
<?php
if($_GET['clean']=='yes'){
	define('DBHost',$dbhost);
	define('DBUser',$dbuser);
	define('DBPwd',$dbpwd);
	define('DBName',$dbname);
	sqlConnect();
	sqlSetDB();
	sqlSetName();
	$sql ="DELETE FROM $dbtable WHERE LENGTH(title)<$titlen OR LENGTH(content)<$contlen";
	$result = sqlQuery($sql);
	sqlClose();
	echo '<div class="box"><h1>清理长度过短的数据已成功</h1>';
	echo '<p>服务器IP:'.$dbhost.'</p><p>数据库:'.$dbname.'</p><p>对应的表:'.$dbtable.'</p><p>标题长度小于'.$titlen.'字节</p><p>内容长度小于'.$contlen.'字节</p>';
	echo '<h3><a href="index.php">返回导航</a></h3></div>';
}else{
	echo '<div class="box"><h1>是否清理长度过短的数据</h1>';
	echo '<p>服务器IP:'.$dbhost.'</p><p>数据库:'.$dbname.'</p><p>对应的表:'.$dbtable.'</p><p>标题长度小于'.$titlen.'字节</p><p>内容长度小于'.$contlen.'字节</p>';
	echo '<h3><a href="?clean=yes">开始清理</a></h3></div>';
}
?>
</body>
</html>

 这时候数据库的效果图:

 

setJSON.php

<?php
require "connect.inc.php";
ini_set('max_execution_time',200);

/******** 配置信息 BEGIN *******/
//Mysql的IP
$dbhost = '127.0.0.1';
//Mysql的账号
$dbuser = 'root';
//Mysql的密码
$dbpwd = '123456';
//Mysql里的数据库
$dbname = 'huocheDB';
//Mysql里的数据库指定那个表
$dbtable = 'tb_demo';

//从哪个id开始,最小的值为 1
$beginID = 1;
//获取多少条数据
$number = 4;
//设置保存的名称,若不重新命名,会覆盖原来的JSOP数据
$jsonName = 'data5';

/******** 配置信息 END *******/
?>
<!DOCTYPE html>
<html lang="zh-cn">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
<title>第一步:保存选中的数据到JSON文件里</title>
<link rel="stylesheet" href="css/base.css" />
</head>
<body>
<?php
define('DBHost',$dbhost);
define('DBUser',$dbuser);
define('DBPwd',$dbpwd);
define('DBName',$dbname);
sqlConnect();
sqlSetDB();
sqlSetName();

$sql = "SELECT 	id, title, content FROM $dbtable WHERE id>=$beginID LIMIT 0, $number";
$result = sqlQuery($sql);

$data = array();
$i=0;
$jsonName = "json/$jsonName.json";

echo '<table cellpadding="0" cellspacing="0" class="g-w">';
echo '<tr class="header"><td colspan="3" align="center">第一步:保存选中的数据到JSON文件里</td></tr>';
echo '<tr class="info"><td colspan="3" align="center">服务器IP:'.$dbhost.' 数据库:'.$dbname.' 对应的表:'.$dbtable.' 保存JSON路径:'.$jsonName.'  从ID '.$beginID.' 开始获取'.$number.'条数据</td></tr>';
echo '<tr class="title"><td>ID</td><td align="center">标题</td><td align="center">内容</td></tr>';

while (!!$row = mysql_fetch_array($result)){
	$data[$i] = array($row[id],$row[title],$row[content]);
	$title = mb_strlen($row[title],'utf-8')>30?mb_substr($row[title],0,27, 'utf-8').'...':$row[title];
	$content = mb_substr(str_replace(array(" "," ","\t","\n","\r"),array("","","","",""),strip_tags($row[content])),0,60, 'utf-8');
	echo '<tr><td>'.$row[id].'</td><td>'.$title.'</td><td>'.$content.'</td></tr>';
	$i++;
}
$json_string = json_encode($data);

file_put_contents($jsonName, $json_string);
sqlClose();
echo '<tr class="footer"><td colspan="3" align="center">已成功添加到JSON文件 <a href="putDEDE.php">点击这里,开始发布数据到织梦后台</a></td></tr></table>';
?>
</body>
</html>

效果图:

 

putDEDE.php

<?php
require "connect.inc.php";
ini_set('max_execution_time',200);

/******** 配置信息 BEGIN *******/
//Mysql的IP
$dbhost = '120.120.120.120';
//Mysql的账号
$dbuser = 'testDB';
//Mysql的密码
$dbpwd = '123456';
//Mysql里的数据库,默认和账号是一样的
//$dbname = 'demo2';
$dbname = $dbuser;


//输入你要发布的JSON数据的名称
$jsonName = 'data5';
//设置栏目ID
//$typeid ='4';
$typeid ='5';
//设置作者
//$writer = 'Muller';
$writer = 'Muller';

/******** 配置信息 END *******/
?>
<!DOCTYPE html>
<html lang="zh-cn">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
<title>发布数据到织梦后台</title>
<link rel="stylesheet" href="css/base.css" />
</head>
<body>
<?php
$jsonName = "json/$jsonName.json";
$json_string = file_get_contents($jsonName);
$data = json_decode($json_string, true);

define('DBHost',$dbhost);
define('DBUser',$dbuser);
define('DBPwd',$dbpwd);
define('DBName',$dbname);
sqlConnect();
sqlSetDB();
sqlSetName();

//获取栏目
$sqlGetType ="SELECT typename FROM dede_arctype WHERE id=$typeid LIMIT 0, 1";
$result = sqlGetOne($sqlGetType);
$typename = mb_strlen($result[typename])<=0?'栏目不存在':$result[typename];

if($_GET['db']=='go'){
	
	//获取插入数据的ID
	$sqlGetID ='SELECT id FROM dede_archives ORDER BY id DESC LIMIT 0, 1';
	$result2 = sqlGetOne($sqlGetID);
	$dataid = $result2[id];
	$dataid+=1;
	
	//创建日志
	date_default_timezone_set("Asia/Shanghai");
	$log = 'log/log '.@date("Y-m-d h.i.s").'.txt';
	$logTxt = '服务器IP:'.$dbhost.' 账号:'.$dbuser.' JSON路径:'.$jsonName.' 栏目ID:'.$typeid.' 栏目内容:'.$typename.' 作者:'.$writer;
	$logTxt .= "\n插入信息如下:\n";
	$logTxt .= "=============== begin ============= \n";
	
	echo '<table cellpadding="0" cellspacing="0" class="g-w">';
	echo '<tr class="header"><td align="center">最后一步:发布完成,不要刷新页面,因为刷新页面会重新发布一次</td></tr>';
	echo '<tr class="info"><td align="center">服务器IP:'.$dbhost.' 账号:'.$dbuser.' JSON路径:'.$jsonName.' 栏目ID:'.$typeid.' 栏目内容:'.$typename.' 作者:'.$writer.'</td></tr>';
	echo '<tr class="title"><td>正在写入日志。。。</td></tr>';

	//循环插入数据
	for($i=0;$i<count($data);$i++){
		//初始化
		//默认插入当前的时间
		$date = time();
		//随机数
		$click = mt_rand(100, 500);
		
		//JSON 数据
		$id = $data[$i][0];
		$title = $data[$i][1];
		$content = $data[$i][2];
		
		//获取描述
		$description = mb_substr(str_replace(array(" "," ","\t","\n","\r"),array("","","","",""),strip_tags($content)),0,250, 'utf-8');
		
		$sqlAdd = "INSERT INTO dede_archives (id, typeid, typeid2, sortrank, flag, ismake, channel, arcrank, click, money, title, shorttitle, color, writer, source, litpic, pubdate, senddate, mid, keywords, lastpost, scores, goodpost, badpost, voteid, notpost, description, filename, dutyadmin, tackid, mtype, weight) VALUES ('$dataid', '$typeid', '0', '$date', NULL, '1', '1', '0', '$click', '0', '$title', '', '', '$writer', '未知', '', '$date', '$date', '1', '$title', '0', '0', '0', '0', '', '0', '$description', '', '1', '0', '0', '0')";
		$sqlAdd2 = "INSERT INTO dede_addonarticle(aid, typeid, body, redirecturl, templet, userip) VALUES ('$dataid','$typeid','$content','','','127.0.0.1')";
		$sqlAdd3 = "INSERT INTO dede_arctiny (id, typeid, typeid2, arcrank, channel, senddate, sortrank, mid) VALUES ('$dataid', '$typeid', '0', '0', '1', '$date', '$date', '1')";
		$sqlUpdata = "Update dede_archives set arcrank =0,ismake=1,dutyadmin =1,tackid=0 WHERE id=$dataid";
		sqlQuery($sqlAdd); 
		sqlQuery($sqlAdd2);
		sqlQuery($sqlAdd3);
		sqlQuery($sqlUpdata);
		echo '<tr><td>JSON ID:'.$id.' 对应的织梦ID是: '.$dataid.' 标题:'.$title .' </td></tr>';
		$logTxt .= 'JSON ID:'.$id.' 对应的织梦ID是: '.$dataid.' 标题:'.$title ."\n";
		$dataid++;
	}
	$logTxt .= "=============== end ============= \n";
	file_put_contents($log, $logTxt);
	echo '<tr class="footer"><td align="center">已发布成功,保存日志文件:'.$log.',请关闭该页面或<a href="index.php">点击返回首页</a></td></tr></table>';
}else{
	echo '<table cellpadding="0" cellspacing="0" class="g-w">';
	echo '<tr class="header"><td colspan="3" align="center">第二步:发布前检查</td></tr>';
	echo '<tr class="info"><td colspan="3" align="center">服务器IP:'.$dbhost.' 账号:'.$dbuser.' JSON路径:'.$jsonName.' 栏目ID:'.$typeid.' 栏目内容:'.$typename.' 作者:'.$writer.'</td></tr>';
	echo '<tr class="title"><td>ID</td><td align="center">标题</td><td align="center">内容</td></tr>';
	for($i=0;$i<count($data);$i++){
		$title = mb_strlen($data[$i][1],'utf-8')>30?mb_substr($data[$i][1],0,27, 'utf-8').'...':$data[$i][1];
		$content = mb_substr(str_replace(array(" "," ","\t","\n","\r"),array("","","","",""),strip_tags($data[$i][2])),0,60, 'utf-8');
		echo '<tr><td>'.$data[$i][0].'</td><td>'.$title.'</td><td>'.$content.'</td></tr>';
	}
	echo '<tr class="footer"><td colspan="3" align="center">确定没问题后 <a href="?db=go">点击开始发布</a></td></tr></table>';
}
sqlClose();
?>
</body>
</html>

 效果图:

 

进入织梦后台查看,如图:

 

猜你喜欢

转载自onestopweb.iteye.com/blog/2341491