- <?php
- //数据库参数
- $$dbuser='';
- $$dbpwd='';
- $$dbname='';
- $$dbhost='';
- $$dbtable='';
- //设置读取路径
- $$rdcontent='xxxx.txt';
- $$rdtitle='xxxxtitle.txt';
- $$rdlink='xxxxlink.txt';
- //读取插入链接,并设置发布概率
- function gen_link($$rdlink){
- $$data2 = file($$rdlink);
- $$num2 = count($$data2);
- $$id2= mt_rand(0,$$num2-1);
- $$random= mt_rand(0,10);
- if($$random<5)
- $$link= trim($$data2[$$id2]);
- else
- $$link="";
- return $$link;
- }
- //生成文章
- $$str = "";
- $$title = "";
- $$data2 = file($$rdtitle);
- $$num2 = count($$data2);
- $$id2 = mt_rand(0,$$num2-1);
- $$title = $$data2[$$id2];
- $$data = file($$rdcontent);
- $$num = count($$data);
- for($$i=0;$$i<25;$$i++){
- $$temp=" ";
- if(($$i%5)==0)
- $$temp="
- \n";
- elseif(($$i==7)|($$i==21)){
- $$links=gen_link($$rdlink);
- $$str=$$str." ".$$links." ";
- }
- $$id = mt_rand(0, $$num-1);
- $$line = trim($$data[$$id]);
- $$str=$$str.$$temp.$$line;
- }
- //生成slug
- function generate_slug($$len){
- $$chars='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
- $$slug="";
- for($$j=0;$$j<$$len;$$j++){
- $$slug .= $$chars[ mt_rand(0, strlen($$chars) - 1) ];
- }
- return $$slug;
- }
- $$slug = generate_slug(17);
- //获取发布时间
- $$datetime= date("Y-m-d H:i:s");
- $$sql= "INSERT INTO `$$dbtable` (`ID`, `post_author`, `post_date`, `post_date_gmt`, `post_content`, `post_title`, `post_excerpt`, `post_status`, `comment_status`, `ping_status`, `post_password`, `post_name`, `to_ping`, `pinged`, `post_modified`, `post_modified_gmt`, `post_content_filtered`, `post_parent`, `guid`, `menu_order`, `post_type`, `post_mime_type`, `comment_count`) VALUES (NULL, '1', '$$datetime', '$$datetime','$$str','$$title', '', 'publish', 'open', 'open', '', '$$slug', '', '', '0000-00-00 00:00:00', '0000-00-00 00:00:00', '', '0', '', '0', 'post', '', '0')";
- //连接数据库并处理数据
- $$con = mysql_connect($$dbhost,$$dbuser,$$dbpwd);
- if(!$$con){
- die('Failed to connect to database! '.mysql_error());
- }else
- echo "Suscess to connect to database!
- \n";
- mysql_select_db($$dbname,$$con);
- $$result=mysql_query($$sql);
- //前面已经插入,获取插入id
- $$insert_id = mysql_insert_id();
- //继续插入wp_term_relationship表
- if($$insert_id > 0 ){
- $$sql= "INSERT INTO `$$dbtable` (`object_id`, `term_taxonomy_id`, `term_order`) VALUES (NULL, '$$insert_id', NULL)";
- $$result=mysql_query($$sql);
- }
- //关闭数据库连接
- mysql_close($$con);
- if(!$$result){
- echo $$result;
- die('Failed to post article! '.mysql_error());
- }else
- echo "Suscess to post article!\n";
- echo $$dbname." is done";
- ?>
复制代码 代码给你改了下,wp_term_relationship表插入的值,你应该还需要改一下,因为我已经忘了这几个表的关系了,剩下你的分享很好,我们一般使用付费cronjob,因为这种东西稳定性很重要,可以搜索setcronjob
如果你需要第1次插入的某些值,你需要在if($insert_id > 0 )下面再增加一个select的查询
前面既然都是插入的内容,那么插入的内容事先就已经知道的,所以没有必要做这个select查询
|