- 5 a6 Z R* n$ p* Q' H, p# w
- <?php
8 s9 r) K3 Q! N5 d9 {" `& M - //數據庫參數
+ u8 H; J8 i6 B - $$dbuser='';7 _1 F! x9 t3 x
- $$dbpwd='';
: p8 \4 n, j" ]! [; q" t2 j - $$dbname='';
) L9 ~5 {/ e2 E5 @1 a2 X6 I - $$dbhost='';! {6 W$ S/ U% T* e% ^ H. Y
- $$dbtable='';
) ^7 r2 s* y4 h# B5 R2 d
. b- z: D) F S" d6 v3 K r9 E- //設置讀取路徑2 X0 `1 n" l. I) ]
- $$rdcontent='xxxx.txt';
( ~. v# E3 i& D- q3 N - $$rdtitle='xxxxtitle.txt';8 f) ~1 I/ n2 q1 o Y( @
- $$rdlink='xxxxlink.txt';( C5 v7 | K: T4 U( F
- 9 o" @/ ~* j7 V. j% ]
- //讀取插入鏈接,並設置發佈概率
9 @9 @& i( s# e, C& O2 c4 V - function gen_link($$rdlink){& K) g6 B! ~2 ~$ J
- $$data2 = file($$rdlink);3 \- d" D( f+ k/ A# m, L
- $$num2 = count($$data2);
: e+ X* z+ K1 R+ |* j5 z) `, d5 y, b - $$id2= mt_rand(0,$$num2-1);, J! f \/ q, O( j: S, m( ?
- $$random= mt_rand(0,10);
% m! e/ ~" T0 S - if($$random<5)4 h" B1 I4 U7 r8 q
- $$link= trim($$data2[$$id2]);" Q( J4 S6 `2 F! m
- else/ |& [8 t% c9 x% u
- $$link="";& l) a4 B1 d6 ~* U0 L
- return $$link;
! \& Z! K- h5 E7 y) S9 r3 l - }
6 _. a2 w$ c& d. k. ?4 y
6 p& \. S/ y0 z' [1 ]; R- //生成文章
$ ]6 x" s9 t" f, r' E% o { - $$str = "";
4 n/ A3 Q) | `: J$ u; ?4 _9 Q - $$title = "";
, j( ?* E3 y3 }8 f# {& ~/ K% C
9 q7 q; U& e x7 S \2 a, C- $$data2 = file($$rdtitle);/ W( K: Z6 K6 P! ?
- $$num2 = count($$data2);
$ x; `$ K Z5 d( p - $$id2 = mt_rand(0,$$num2-1);5 x& _9 g2 E- h0 B
- $$title = $$data2[$$id2];7 U7 f, V% K o1 N
9 O' S; ]) w0 \/ e0 K- $$data = file($$rdcontent);
$ a4 F C; ]4 k - $$num = count($$data);
8 M: l- |" U3 w8 { w/ d8 q# d- s - ! f2 S* ^" t: L; O
- for($$i=0;$$i<25;$$i++){1 x# b! T' P: T: d9 s6 L
- $$temp=" ";) W. |; m2 w, }8 ?+ l) s4 L
- if(($$i%5)==0)' s' U# u( c9 P. J8 ^
- $$temp="
& o& E3 m& z& m8 _5 c - \n";
& z* g" R! }+ }" X# Y2 F2 B3 @ - elseif(($$i==7)|($$i==21)){; G* ]# W0 Q2 y, p4 `, W
- $$links=gen_link($$rdlink);
+ E, F" t% H8 ~ - $$str=$$str." ".$$links." ";: ~% @4 W0 Z+ f3 g
- }2 U3 l' o5 `$ {
- $$id = mt_rand(0, $$num-1);+ ?% ?6 j; O5 K7 i9 p
- $$line = trim($$data[$$id]);. g) \ y( W% v+ z/ _& M# p
- $$str=$$str.$$temp.$$line;3 Q2 `" x3 v8 C* I
- }
4 ~ |. j# |9 Q$ J) X
% I( _7 F! i5 M- //生成slug8 ]4 u1 _6 U/ m
- function generate_slug($$len){7 y' }' i6 m( W9 w6 k
- $$chars='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';% C3 w: x+ R& X0 }# i( _
- $$slug="";
+ G2 }4 l4 N( j/ H# R: a6 q) O7 W - for($$j=0;$$j<$$len;$$j++){
+ f4 [. j( d9 S4 K - $$slug .= $$chars[ mt_rand(0, strlen($$chars) - 1) ];
3 D5 R* L* `, o" T3 }1 S- j - }& c1 E9 m3 Y: ]" f- y4 U: m
- return $$slug;
. b. G( @* J, J - }
6 B3 y: H2 ?' x F) R9 x( F - $$slug = generate_slug(17);1 q7 @ ]) R# z" B
/ t9 I5 S) x. z2 P& e6 j I) `- //獲取發佈時間! C# i7 I; t3 c2 u
- $$datetime= date("Y-m-d H:i:s");: C0 ?+ w: ?: `$ r }5 H2 J: M1 z) N q( H
- ( i& I% [3 @" E9 d) p8 U% f
- $$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')";7 o! T, @2 o$ x0 g4 q8 {+ X
- : O9 O8 C8 D: z' a
- //連接數據庫並處理數據
u' ~' @+ M( {, `4 C - $$con = mysql_connect($$dbhost,$$dbuser,$$dbpwd);
1 ~8 r0 ?: @/ s8 _ - if(!$$con){
4 Q& ^- b. P- K4 |. t - die('Failed to connect to database! '.mysql_error());* z& \5 { D b
- }else
4 C" _/ E% ]0 b/ F - echo "Suscess to connect to database!
, [/ x& ]$ K7 f9 m/ T - \n";
i _ K, P) R1 |; |3 w7 V - 2 l6 m. \1 C) {/ e! L
- mysql_select_db($$dbname,$$con);. x7 |: |2 X4 D: A+ f; t) n" |- F# t
- 8 M0 ?/ S! i5 i' R2 P6 P( j/ b) C' H
- $$result=mysql_query($$sql);
! j! `2 o7 _, S- X. N2 ~# K, \
! U" I8 V- x; `2 H2 G- 9 `) |( X7 z( G; u/ `0 `) |
- //前面已經插入,獲取插入id
" K0 l. ]) Q/ E9 S7 } - $$insert_id = mysql_insert_id();4 G' H( {9 O. @+ @' _' q5 v
- ) H% E8 o: u/ M
- //繼續插入wp_term_relationship表
9 a1 G3 o: l. g) s8 |* L; `: K - if($$insert_id > 0 ){7 k+ w3 u a+ d4 {/ ^
- $$sql= "INSERT INTO `$$dbtable` (`object_id`, `term_taxonomy_id`, `term_order`) VALUES (NULL, '$$insert_id', NULL)";# a( v) ~( ~6 h1 I3 p9 L3 m
- . I$ f1 P! @, d( A3 M+ u
- $$result=mysql_query($$sql);
9 F* l: H7 Z: a, n3 C$ R - }3 V9 L% k2 O q2 `% W; c
- //關閉數據庫連接( Q8 u" M* g! F B/ o. j# H
- mysql_close($$con);
) F2 R$ c5 \& A$ V9 U" m - ' w6 {/ b1 G% p9 j
" o: [6 ^8 Z0 M' [4 w, R* v- 5 Q; {* \" H+ z9 u* M
- if(!$$result){/ g7 B! q u ~0 @ W
- echo $$result;$ i! R8 b6 C% ~+ a
- die('Failed to post article! '.mysql_error());/ R, S& m3 [6 p' T1 h" v/ k
- }else
8 o% r; @ r0 K0 q) Q - echo "Suscess to post article!\n";. W' g4 v) w G9 f8 o
' L9 R: J0 N5 H: K- X) l- echo $$dbname." is done";
0 ]- v4 L" O" R) _ - # h' [. \9 k* G
- ?>5 d4 C0 N! O# N+ J U( J
- , n, H1 w' f# l5 n% D+ y$ m _' K
- $ w) `8 \6 v* k1 Q9 F1 w
- 2 m& t7 |! f4 d3 ^; f1 {, P
9 @2 G. q/ @- k) y' z; I
複製代碼 代碼給你改了下,wp_term_relationship表插入的值,你應該還需要改一下,因為我已經忘了這幾個表的關係了,剩下你的分享很好,我們一般使用付費cronjob,因為這種東西穩定性很重要,可以搜索setcronjob1 ^# U' P8 I, E" S6 z6 c' }8 r
5 G. e3 s' Y7 x+ T# I1 s: [( @# ?( K- F' t
如果你需要第1次插入的某些值,你需要在if($insert_id > 0 )下面再增加一個select的查詢
9 B& f+ i. B7 ]2 v/ p前面既然都是插入的內容,那麼插入的內容事先就已經知道的,所以沒有必要做這個select查詢& }9 F' V9 f! D' E9 }$ E3 t
! D1 e& V4 X% h0 E; H
|