本帖最後由 hudba 於 2013-6-17 02:45 編輯
1 h! C9 D" v6 a1 p# M+ R# |
$ H% p2 \# N/ K6 T以前用到程序中要調用wordpress api的地方,探索了一下,這裡整理一下發給大家,但願對有需要的朋友有幫助。
) V" Q# m1 T' w% g/ x2 S& d: F u2 W8 w5 W1 j
準備工作:
* z+ K" ]' [& L+ N3 D' @& c使用C#調用,推薦vs2010,這裡有下載:
1 F' Y4 x1 P) u* s0 \http://www.microsoft.com/en-us/download/details.aspx?id=12187
. C. O$ Z& U# G5 Z程序和wordpress通信需要使用xmlrpc,需要從這裡下載獲得,(或者從我附帶的壓縮包裡也可以獲得):
1 E" w" y* |5 u. v+ W5 mhttp://xml-rpc.net/download.html
/ q; i; p) T0 N0 _wordpress api的文檔:
% a3 t- {& ~0 }, N) x' mhttp://codex.wordpress.org/XML-RPC_WordPress_API
" I7 C% m7 c$ X1 D# t* K ?要點講述:
) u6 V3 }1 C3 g0 Tvs裡面建立solution:WordpressExample,然後引用xmlrpc的dll,如下圖:
( Y& H( t: j1 o! |% [* G
9 q# h+ D( C" B: l. }" g$ C( Y9 l; x9 @/ `5 f
如何新建Post?4 L( \' G( P6 W8 j9 ]# g4 `; c8 m
查看wordpress的文檔,找到newPost操作需要傳入的參數:8 Q+ U y6 V- i Z: f: W0 D9 i, r# s
http://codex.wordpress.org/XML-RPC_WordPress_API/Posts#wp.newPost ; s7 E3 ~% Q( _$ D
$ _4 Y" p* K& A6 K+ `: ^, ]$ C4 B% X其中,blog_id, username, password是每次調用api都需要傳入的身份信息。如果不是mu版本,blog_id使用0。struct content部分是針對每個操作的具體的參數。不過不是每個參數都必須要傳入。
) d# S; n2 a& a! ~: D
: k6 E+ {5 W+ P& p6 u! [1 x+ z定義api調用接口:
' L1 y9 q( ?; j3 B3 P6 [# l調用我們使用的xmlrpc類庫,需要建立一個從IXmlRpcProxy繼承來的interface,我們這裡取名叫IWordpress,然後把調用的每個api操作的method寫出來,因為此類庫使用了反射,所以參數的名字必須要和文檔裡面一樣。api的名字使用屬性標識,例如:[XmlRpcMethod("wp.newPost")],這樣你interface裡面的方法名稱可以取一個可讀性高一點的名字。- public interface IWordpress : IXmlRpcProxy: n3 m4 l j" P8 m
- {& ]1 Y5 d' e1 @: ]% D
- [XmlRpcMethod("wp.newPost")]
5 c6 D0 s, y3 k/ K( z& t - string NewPost(int blog_id, string username, string password, NewPostInput newPostInput);6 }( ~( N B" {0 R3 k2 Z
- }
複製代碼 建立一個方法方便程序裡面調用:- public static string NewPost(string url, string username, string password, string title, string content)
. n( f; b& g5 I( o7 w - {
# p7 c" c; h! F% J# p. U b0 ^ S - IWordpress proxy = (IWordpress)XmlRpcProxyGen.Create(typeof(IWordpress));9 T/ R3 ~0 i2 n! x, x* b0 F N
- proxy.Url = url;
6 j* I* j% H4 A( ]* z& T' [/ h2 ?. k! w/ G - NewPostInput postInput;
! ~0 z8 O0 C4 Q0 T. \! k - postInput.post_title = title;* e; Q2 e! _* I' H5 N
- postInput.post_content = content;+ U5 P' m1 ]2 }: D. U+ P
- postInput.post_status = "publish";2 k0 l" ?' q3 }' [/ W
, f; O+ q3 m( F- string postId = proxy.NewPost(0, username, password, postInput);2 b# ~; r1 g' u9 m5 q
- return postId;7 D* z4 d$ w* L( l {, N9 P. g
- }
複製代碼 這裡我們首先通過XmlRpcProxyGen.Create創建了一個剛才IWordpress接口的對象,然後指定wordpress url和傳入參數。( ]/ b. m% v) r# ^( @9 r
其中,作為Post內容的參數是個復合類型,所以我建立了一個struct來表示:- public struct NewPostInput
L0 G+ V/ \: Y; J& ~ - {
/ O" F4 J6 K" B - public string post_title;7 I$ Q2 ^; Y; d. J. L' a. \* U, U
- public string post_content;
) D0 @) p$ b [: ^ - public string post_status;0 Y6 `5 t. B. |7 c8 ^. l
- }
複製代碼 文檔裡面的很多參數是可選的,所以作為例子,這裡只有3個主要參數。注意,參數的名字要和文檔裡面的一樣。 x e6 b* D' {) x/ |$ a1 q2 Z) A
; y9 L% I0 i: @# l+ ?* z) u
如何獲取Post列表?
: Q% y3 ?, o& R( c( Z8 K9 q0 i( L同NewPost一樣,先查看文檔,然後把api的定義寫到IWordpress裡面:- public interface IWordpress : IXmlRpcProxy
& F2 _& @2 ?) U+ g+ }# O - {; m' X$ K1 z. W$ A3 z ^! I
- [XmlRpcMethod("wp.newPost")]! S! \ G) X- Y" K2 s6 j
- string NewPost(int blog_id, string username, string password, NewPostInput newPostInput);# i- V R2 r4 A4 J
3 ^7 K% l! e; M* S- [XmlRpcMethod("wp.getPosts")]$ `) W. ~& t: {6 O( N: j& F
- XmlRpcStruct[] GetPosts(int blog_id, string username, string password);
+ J; K& b: {) [, a x0 V. J% h6 O& X - }
複製代碼 接下來,建立一個方便用戶調用的方法,GetPosts:- public static XmlRpcStruct[] GetPosts(string url, string username, string password)
1 V1 B! c- e$ T# S! L( @9 x - {" ^% m0 q0 O9 u) ]( q8 E+ L
- IWordpress wordpress = (IWordpress)XmlRpcProxyGen.Create(typeof(IWordpress));
( t0 Z s+ ?1 ?; I7 ^( @9 s5 M - wordpress.Url = url;+ @+ \- v# g' p9 M. e
- XmlRpcStruct[] ret = wordpress.GetPosts(0, username, password);9 o$ b1 x' O @/ I5 P5 e4 x) ~
- return ret;3 F- O3 n9 j4 b' _) N
- }
複製代碼 這裡返回的是XmlRpcStruct數組。XmlRpcStruct是可以通過字符串索引內容的,比如:ret[0]["post_title"],可以查詢的字段名稱在文檔的return values裡面有說明:
% ~1 w4 [; U$ W2 T8 i$ Zhttp://codex.wordpress.org/XML-RPC_WordPress_API/Posts#wp.getPost 1 G9 E# _" a o- K
' u1 { B3 i" X
" b# n: B7 w. T9 Z7 z調用wordpress api的url是什麼?
4 Y) @& `( C- |wordpress的安裝目錄下面有個xmlrpc.php,調用的url就是這個文件的web地址,比如:
) K* V/ V7 r f% Y: u8 Nhttp://www.example.com/xmlrpc.php, K+ k4 v8 x v) ?0 h; e
9 r6 p8 T: u7 ]% H) M5 m, r4 G
很少寫東西,一動手才感覺簡單的東西,自己雖然知道,但是表達出來挺彆扭。(由己知彼,對寫出一大堆教程的moon light更加佩服了 )。
" D0 D& T4 y% H! h( q+ X' `希望能對大家有幫助,為有這方面需要的朋友節約一點時間,附上源代碼供大家動手試試:
' U$ h/ l6 K& V" W! P$ U
WordpressExample.rar
(50.22 KB, 下載次數: 7)
" R1 Z0 ~8 p$ E/ R! w$ X# ?9 L3 x
% s- e' G% W) A, S& {7 T; x' k3 j/ U3 `! d
/ Z( m1 O6 B# p q5 {& H" S
8 ?" n$ B3 c, K' p
: _& o2 h0 Y8 ^
0 _8 N; _0 [2 \ t m |