0 y! R/ G1 k2 i) ], Q' I( U8 o
: S) B1 H5 T; Y0 @
其實就是ajax檢索
4 m. A3 G# m. l; L3 H( p
. l5 w. U. `3 Y+ i0 Q8 Y<?php
5 j8 p$ W* F* u$ O! L3 |
2 [. c+ A* P! [+ j0 P$word=$_GET['word']; //查詢的關鍵詞# k" l( C6 H2 a# X( A( V3 X
# A& E2 z+ K; [if ($word=="")8 M% ?4 x( W* Y6 m
{% Y$ V5 i. o& ?( [9 v; J5 u/ K
echo <<<EOF
' H, x% U" s) u0 t( f<form action="" method="get">7 B2 z5 U* |% o& ]0 q$ i# Y7 v
<p>關鍵詞: <input type="text" name="word" /></p>
8 ^% F4 j+ m& m& Z9 g: _- P1 T5 r <input type="submit" value="採集" />
7 `1 J( P5 r0 ^. a8 h8 u3 U</form>+ `% a1 T: w; ^: ?: q5 |' ?5 d
EOF;
# r% @7 w' w& g W9 @. e}
% Z8 W. [- i8 I: ?( T, Helse3 _2 N2 Q8 A. ]" W8 a4 o! c0 q
{$ d9 C' F3 m* Q+ m0 c0 \& A0 g
0 U0 o" q6 f6 y- C, {1 S' n
$data=file_get_contents('http://suggestion.baidu.com/su?wd='.$word);//讀取相關關鍵詞所) {; H6 J. e$ s0 x4 \: V& y# Y4 u3 X5 z, f: I
$data=mb_convert_encoding($data, 'UTF-8', 'UTF-8,GBK,GB2312,BIG5' );3 j, P0 E* [4 i6 U$ {
$data_temp=strpos($data,"x");
5 h- A+ T3 F% C* s$data=substr_replace($data,"",$data_temp,17);
- k* L% t8 E# X; L$ x7 a- W$data = trim($data,");");
0 p6 f* B* j, V1 f; v$data = trim($data,"{");6 [* G' p& ?/ W9 e2 ^. M; ^( R
$data=preg_replace("/q:.+?.e,/",'', $data);
7 r. Y+ u; b: h& s# J* g' R0 I$data = str_replace("[","",$data);8 ]0 Q$ ~/ w; l8 X, A5 E
$data = str_replace("]","",$data);
$ I; ]+ N3 ~* H+ t4 x8 q6 Q$data = "[".$data."]"; a! C9 p# d! T; s$ z: ^' e9 B
$data = str_replace(",","},s:",$data);
* y. g. B# ?. D1 a" x$data = str_replace("s:","{\"s\":",$data);//複雜的處理,以符合json格式) ^& U! W+ V& M. F
$dc=json_decode($data);( S; z, |. t! T5 `" G
for ($n=0; $n<=9; $n++)
5 n) ?% Z7 ?7 o5 E7 _5 \4 s{
% ^" a, `- g( o: \$wd[$n]=$dc[$n]->s;7 C- C/ g: m7 W- d" L- w
echo "</br>".$wd[$n];
8 d" {2 W: l- Z8 r}
6 k7 C. c; u' i/ M0 }! H8 L) J) I# Y7 M
}
6 I( S$ M) U( ^8 T0 q0 I: H8 r1 k?>
4 F6 ]" @: @- j$ W! Z: }" d9 O! K, A0 J0 b/ }
7 j9 X6 c# M- X7 G \+ U
8 o) ]3 A9 Y# x$ e |