本帖最後由 hudba 於 2013-6-17 02:45 編輯
1 f. y. m1 y/ ? Z$ U
/ \8 Q$ x; n- L! b以前用到程序中要調用wordpress api的地方,探索了一下,這裡整理一下發給大家,但願對有需要的朋友有幫助。4 k/ W1 I" l- Q, P
/ V8 w @1 L5 d/ q1 p0 P2 _9 W
準備工作:
5 m8 Z+ ~+ b5 f+ J$ |使用C#調用,推薦vs2010,這裡有下載:
3 f5 U0 g0 P( w- b5 whttp://www.microsoft.com/en-us/download/details.aspx?id=12187
6 Z1 T4 _9 B) s% R7 M/ w0 w程序和wordpress通信需要使用xmlrpc,需要從這裡下載獲得,(或者從我附帶的壓縮包裡也可以獲得):+ ?( I. H, y/ Z- B: k T* Y
http://xml-rpc.net/download.html 3 k7 G0 N7 y* i& L0 c3 M1 p& K
wordpress api的文檔:
/ ^8 E2 A- s$ t, y8 ?http://codex.wordpress.org/XML-RPC_WordPress_API
$ O. [- m. u, T! q要點講述:+ C& {: _' h Q: P2 }% n
vs裡面建立solution:WordpressExample,然後引用xmlrpc的dll,如下圖:+ V( f# T: Y$ Z7 e
. l. u2 r' l! N+ n' l% n: h! m* \8 J" ^) F/ G% M! n
如何新建Post?& p' k& H1 K/ |! N" q, d6 P, G* x
查看wordpress的文檔,找到newPost操作需要傳入的參數:! p1 Z; Z& ] d9 q0 m
http://codex.wordpress.org/XML-RPC_WordPress_API/Posts#wp.newPost
* k( {8 K2 |0 x# m% t
5 a' f, s. ]5 r( ]4 A; G3 J6 E其中,blog_id, username, password是每次調用api都需要傳入的身份信息。如果不是mu版本,blog_id使用0。struct content部分是針對每個操作的具體的參數。不過不是每個參數都必須要傳入。
0 X' g/ b$ p- f1 C! l6 F, Q- x, G7 a: \* M
定義api調用接口:0 s! t5 R# A; [/ Z9 h3 v) F
調用我們使用的xmlrpc類庫,需要建立一個從IXmlRpcProxy繼承來的interface,我們這裡取名叫IWordpress,然後把調用的每個api操作的method寫出來,因為此類庫使用了反射,所以參數的名字必須要和文檔裡面一樣。api的名字使用屬性標識,例如:[XmlRpcMethod("wp.newPost")],這樣你interface裡面的方法名稱可以取一個可讀性高一點的名字。- public interface IWordpress : IXmlRpcProxy2 N- r2 G6 d* Z" [7 h% h+ P
- {% t# z% L; e, R* C+ q
- [XmlRpcMethod("wp.newPost")]8 V5 h; Z! k0 g! L3 w8 }, Z7 U
- string NewPost(int blog_id, string username, string password, NewPostInput newPostInput);
; ?- Y9 ^4 {- H- o/ ~. u - }
複製代碼 建立一個方法方便程序裡面調用:- public static string NewPost(string url, string username, string password, string title, string content)7 r+ K0 \! I5 q7 V& |* m
- {
' Y7 g& S5 \5 w' N' g - IWordpress proxy = (IWordpress)XmlRpcProxyGen.Create(typeof(IWordpress));
) P8 U# `( i# v; x; s! t, v - proxy.Url = url;1 C" ~! f/ a- O) o& ?
- NewPostInput postInput;
, e" _* G# b Q, S - postInput.post_title = title;
% r. [6 A7 v4 Q8 ? - postInput.post_content = content;
; B( h' k4 m7 _( M {) d - postInput.post_status = "publish";/ x# D+ m2 y+ x; G7 b
- - u$ C) m$ K! w$ c* J5 p
- string postId = proxy.NewPost(0, username, password, postInput);7 t+ `5 ^6 u% B L
- return postId;6 _- W+ o8 q0 \
- }
複製代碼 這裡我們首先通過XmlRpcProxyGen.Create創建了一個剛才IWordpress接口的對象,然後指定wordpress url和傳入參數。
! L- C* o9 T7 A7 F其中,作為Post內容的參數是個復合類型,所以我建立了一個struct來表示:- public struct NewPostInput' X8 F) }$ U5 B6 x* J4 k; t5 \' F5 ]
- {
2 ~+ v. w4 Y9 ~7 {; T - public string post_title;
4 P) ]$ j0 @9 v$ ` - public string post_content;3 ?% Y. m. l/ e3 s- Z+ H6 A4 g
- public string post_status;
" E; u% F7 p" f9 ] - }
複製代碼 文檔裡面的很多參數是可選的,所以作為例子,這裡只有3個主要參數。注意,參數的名字要和文檔裡面的一樣。
+ u# i$ W' B0 P' I; g: U# k7 }% @' x" q x+ S
如何獲取Post列表?8 f3 W" @7 n" p) S
同NewPost一樣,先查看文檔,然後把api的定義寫到IWordpress裡面:- public interface IWordpress : IXmlRpcProxy# S+ H& e5 L7 i2 @$ M, `# k' ?: G5 @
- {( M6 T, I/ K2 c
- [XmlRpcMethod("wp.newPost")]
( t) N3 S4 \: N& I @ - string NewPost(int blog_id, string username, string password, NewPostInput newPostInput);
% Q: J( {. v% k9 l4 V! V3 z$ V - " X0 B3 F# O4 W
- [XmlRpcMethod("wp.getPosts")]
* X. h5 @8 v2 w8 q2 U% ~* V - XmlRpcStruct[] GetPosts(int blog_id, string username, string password);
- V8 ^. |+ f x+ v - }
複製代碼 接下來,建立一個方便用戶調用的方法,GetPosts:- public static XmlRpcStruct[] GetPosts(string url, string username, string password) v N1 q2 @/ H2 X$ D4 Y: c; o
- {' u/ V8 c( P* q5 ?" G+ o
- IWordpress wordpress = (IWordpress)XmlRpcProxyGen.Create(typeof(IWordpress));
: h$ u: S; \0 K- x - wordpress.Url = url;! r+ I# l$ ^3 c7 [# C
- XmlRpcStruct[] ret = wordpress.GetPosts(0, username, password);
& f. c- G7 V# `# t1 }0 i/ Z+ v - return ret;
- j' A2 P/ T4 i, c `/ |' R - }
複製代碼 這裡返回的是XmlRpcStruct數組。XmlRpcStruct是可以通過字符串索引內容的,比如:ret[0]["post_title"],可以查詢的字段名稱在文檔的return values裡面有說明:
% }& K7 s: G/ C! [' `2 J0 z+ Bhttp://codex.wordpress.org/XML-RPC_WordPress_API/Posts#wp.getPost # I: o4 f) K; ]: z2 T5 f) A
9 V) I/ ?, i4 Z* ]+ P
( t, x' z% e7 D6 g% D調用wordpress api的url是什麼?
# z% K2 `5 H3 e& Y$ B1 Wwordpress的安裝目錄下面有個xmlrpc.php,調用的url就是這個文件的web地址,比如:
+ o3 N' _) h/ b/ k/ shttp://www.example.com/xmlrpc.php
% `6 T, P4 m/ W& U. ~
$ h% @0 T; `3 r- W$ B. }很少寫東西,一動手才感覺簡單的東西,自己雖然知道,但是表達出來挺彆扭。(由己知彼,對寫出一大堆教程的moon light更加佩服了 )。. z9 r* O- J* y H/ S% N) O' K
希望能對大家有幫助,為有這方面需要的朋友節約一點時間,附上源代碼供大家動手試試:
5 r! p9 E" Y- G1 P$ Z# \
WordpressExample.rar
(50.22 KB, 下載次數: 7)
0 \ B; F# Z; |; }. O U! ^) r( p6 Y5 O
3 k, e) b2 f2 X7 F% ]8 h8 z, p: \1 s3 V, ?) `2 a7 C
Q8 C+ ^7 [$ W" _. P) e# v6 z/ w4 L& `: b" [4 Q9 l
, ]: {4 q" p# i# e( v7 L y4 K
|