4 [7 U* z5 J7 Q6 c+ v- <?php$ T9 c+ I5 e- K' _1 ]4 V, N
- //數據庫參數. w) r5 N P. \! g# ^/ A
- $$dbuser='';6 C9 `5 R; q8 X
- $$dbpwd='';- W1 q* g |( f# u
- $$dbname='';
, X$ X5 _+ U6 V) Q+ K/ j9 V/ q% G5 w - $$dbhost='';
* Z0 X4 k( Y& S9 r - $$dbtable='';
8 O0 I" |; D, x8 B - # d+ c+ d X i9 D. C1 s
- //設置讀取路徑/ }# y& K, G6 ?5 y5 G' |6 D9 l5 A4 g
- $$rdcontent='xxxx.txt';5 ?+ g5 e" c! U. n% M
- $$rdtitle='xxxxtitle.txt';) |, d( J" T/ K2 t' U
- $$rdlink='xxxxlink.txt'; u/ ]4 a% w4 P: q+ Y: g" @( r! ^
- 9 N# n4 D5 d0 z
- //讀取插入鏈接,並設置發佈概率
( _0 r! T! F- n; A, X7 ~7 u9 O - function gen_link($$rdlink){
$ j/ T7 X! ]; R4 e8 `; J* M. k0 k4 F - $$data2 = file($$rdlink);' C' Q4 i& {8 j' u8 h2 H- }
- $$num2 = count($$data2);
: }8 V3 O/ M9 V; B& {7 S/ B g - $$id2= mt_rand(0,$$num2-1);2 O2 F5 o. S: Z- K0 g, R
- $$random= mt_rand(0,10);
. M$ [. d7 f# O8 O( w% q& e) [1 g - if($$random<5)9 u. ^- [/ \0 m* ~) |7 ?$ j
- $$link= trim($$data2[$$id2]);
( o% S6 ]0 [: {- a* j - else% _3 Z0 ?* ~% M& x
- $$link="";
9 G0 P5 V/ a. s - return $$link;5 y; M5 A3 U, t3 H; }) O
- }% d0 x9 e! G: A& N; H$ Q. @9 W& u
5 S4 }2 w4 k$ |' f' y7 {- //生成文章
3 |+ v# v! ~. k; R* [4 q- r1 E" V - $$str = "";6 }/ ~) f. l' q6 q
- $$title = "";! Q: p: p) Y1 d. Q9 j8 g' n+ ~
. _, _9 s( D& C: A F* u# K5 `- $$data2 = file($$rdtitle);
3 |0 Y4 n d/ N) B* t - $$num2 = count($$data2);
) A/ D/ ]& `- J - $$id2 = mt_rand(0,$$num2-1);
0 r) D l0 o* a x0 z - $$title = $$data2[$$id2];- }( `4 m( R) `7 _+ ^; m
- 0 ~/ }. X6 K8 u+ h& L- G
- $$data = file($$rdcontent);
; y5 b2 M( ?5 [% l: _/ H - $$num = count($$data);
/ {% Y4 X' m+ F* {& D3 d6 i, l& [& y - J3 t6 m) O+ |# \8 {* }# c2 z$ v; _
- for($$i=0;$$i<25;$$i++){; Q4 O! D) O# H" _5 B3 G. Z2 |; G
- $$temp=" ";
4 E7 a' E7 J1 Z% v% H, `$ a - if(($$i%5)==0), ^' a$ p* H/ U* O& S7 x3 S
- $$temp="6 g. Q6 b' U- p$ x3 k- Y
- \n";
. U, Q) A. u4 S* n+ E1 h - elseif(($$i==7)|($$i==21)){! C& }6 U$ c# \3 r/ p+ ]9 r3 C
- $$links=gen_link($$rdlink);
7 l o1 D! J& A; G* ] - $$str=$$str." ".$$links." ";
7 N; w3 s8 g. D( u! f$ o4 ~8 n - }
3 `% S' K. R2 E9 L2 W# Y - $$id = mt_rand(0, $$num-1);
+ Y/ n# M, @5 m5 t; o/ C4 u - $$line = trim($$data[$$id]);6 ]% Q, [, X! p
- $$str=$$str.$$temp.$$line;
7 p) U, u+ c( v6 N, T3 Q - }) j2 K! y% a1 T! u& g6 G Y# k6 [- n
9 F$ z' }# y; v' ~1 E; V- //生成slug
s" _2 i3 i! C. v- l! l - function generate_slug($$len){
Q( P6 s+ u, \8 H# Y; g& Q - $$chars='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';6 l9 `! b6 h' G% q# e4 b
- $$slug="";
$ W; X7 J' Q: O" A& y - for($$j=0;$$j<$$len;$$j++){4 J8 v( F+ }4 m4 i
- $$slug .= $$chars[ mt_rand(0, strlen($$chars) - 1) ];" i, e- W" T. V8 G* A; n4 A
- }
, `, E8 [2 H! V* }% d3 k - return $$slug;
- V+ H* G7 v! v - }# c6 C) F; G- G" ^% k; Y0 l
- $$slug = generate_slug(17);( v& V* Z+ l. I' h# `2 o. M
- - S. K) U6 W5 g
- //獲取發佈時間
e9 ]2 \* i' w% b4 f3 [5 U3 ] - $$datetime= date("Y-m-d H:i:s");. [) e( O; H8 d1 d4 V+ k3 j/ h! }
- ) l9 K* `9 T3 l/ X9 ]
- $$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')";
$ I" a+ v( z+ D4 @4 b, t5 h - ( T5 ]- \+ w* A( N
- //連接數據庫並處理數據
! ?+ y5 b. Z: {4 H2 X% w( N2 w - $$con = mysql_connect($$dbhost,$$dbuser,$$dbpwd);
- w h3 e3 ~$ A - if(!$$con){
% j- k6 R' V. w& B. U { - die('Failed to connect to database! '.mysql_error());
. q% U6 ?1 ^; S T/ M7 \ - }else
$ n4 e5 S* O. r! d5 C3 Z% J+ f - echo "Suscess to connect to database!, P5 _; ^4 E, U* `' w$ X
- \n";
. I" {3 Y7 A8 \3 _1 W
) S' ]6 Q* n8 \$ {, e+ n9 \! p- mysql_select_db($$dbname,$$con);
) b+ O7 r3 p# z) A% q - ) a; z- O+ S' I4 K
- $$result=mysql_query($$sql);
- ~# b% q) p3 D, m/ x
) {: D/ D" t q
9 D3 E; x- l/ p/ q3 O8 C; K- //前面已經插入,獲取插入id9 m* t8 F6 b, q; J# I2 ?
- $$insert_id = mysql_insert_id();1 A2 s+ z& D+ G" H/ R
- / V# g, R$ Z6 b5 j
- //繼續插入wp_term_relationship表- c, ^7 g& C2 x3 y1 d" ~
- if($$insert_id > 0 ){2 \# r! q: i2 {6 d [
- $$sql= "INSERT INTO `$$dbtable` (`object_id`, `term_taxonomy_id`, `term_order`) VALUES (NULL, '$$insert_id', NULL)";
- X) N5 M' e A- k9 a: v' S% y; E - 2 u8 z; P" z y4 c7 H, N
- $$result=mysql_query($$sql);" {7 E9 o# E! N* H! j& l4 _# \# D
- }
: r; M7 @8 H) l - //關閉數據庫連接
" n g2 ]/ K! a% \4 H2 k0 s! `5 P - mysql_close($$con);# |2 f: x4 C( v5 U/ t8 t
( d v) I8 s6 t( X/ B6 k. K3 N5 }- 2 V( F9 c1 h. p
. ~. T! J& [" y1 r% {: f- if(!$$result){4 ~4 ^8 x- z$ X
- echo $$result;
7 N( g0 v0 O3 c+ @% i' o$ y - die('Failed to post article! '.mysql_error());/ g1 |) D' m2 D8 E! O
- }else! A4 U7 ~3 O: B/ \# A
- echo "Suscess to post article!\n";
7 ^" z+ X* n+ L/ J+ X7 r. A# k - 7 G& N! T. `6 g! l$ l, p H
- echo $$dbname." is done";
) N) w5 P& ?$ m. g+ c - 2 g0 D5 j2 n+ ^0 T7 K
- ?>! O, y* F) w$ p4 U; S: ?0 x
- K! z+ b1 K/ N) l
% f8 Z6 w7 ^/ ~2 U3 J& @7 }; v9 i+ B
+ q/ g, N9 Y& Z) I% D; T' G* Q4 D- ( L+ R: q( C- n( u* ]" q
複製代碼 代碼給你改了下,wp_term_relationship表插入的值,你應該還需要改一下,因為我已經忘了這幾個表的關係了,剩下你的分享很好,我們一般使用付費cronjob,因為這種東西穩定性很重要,可以搜索setcronjob, U' c$ w, h/ {8 o
/ f4 W1 b* ]" ?0 z: j8 ~
" Y9 Y' P K. m8 ?9 c
如果你需要第1次插入的某些值,你需要在if($insert_id > 0 )下面再增加一個select的查詢
5 g% r# p' F Z N) x8 ]. R' l& Z前面既然都是插入的內容,那麼插入的內容事先就已經知道的,所以沒有必要做這個select查詢
% T4 Z0 l2 A3 ?4 ]- P, B1 K
8 p$ d* F9 U# u" | |