本帖最後由 hudba 於 2013-6-17 02:45 編輯 [1 z; {; a$ r) }+ s1 _1 m
5 v* R' V/ {6 ]5 B以前用到程序中要調用wordpress api的地方,探索了一下,這裡整理一下發給大家,但願對有需要的朋友有幫助。- B$ a2 i. u! }( K" E' I
+ S) Z* i n8 G7 j2 i
準備工作:# O" K" V2 R& W/ s
使用C#調用,推薦vs2010,這裡有下載:' o. r* j* W" n! c
http://www.microsoft.com/en-us/download/details.aspx?id=12187
g. ]5 E" ]$ @' n3 \, ?程序和wordpress通信需要使用xmlrpc,需要從這裡下載獲得,(或者從我附帶的壓縮包裡也可以獲得):/ e( a$ [5 b$ U
http://xml-rpc.net/download.html 4 Z6 L, H+ |/ P2 {& {: I* d3 R. b
wordpress api的文檔:
, o/ C3 {1 l' z2 X' i) j( xhttp://codex.wordpress.org/XML-RPC_WordPress_API
1 {1 i8 u+ n- _, u$ p" }9 ^* t" Z$ p要點講述:* I+ h M; }8 Y2 O3 V
vs裡面建立solution:WordpressExample,然後引用xmlrpc的dll,如下圖:
8 Q! Z) J: ~5 a2 R6 c x
9 K+ p; o+ ]+ u8 u% q+ w& @
3 n0 K0 M# L- e+ Z2 u* L6 y! Z5 R C如何新建Post?' f+ \& \$ a9 H& ]5 t; u) Q9 j
查看wordpress的文檔,找到newPost操作需要傳入的參數:! q* V5 X" D" c T2 }" e
http://codex.wordpress.org/XML-RPC_WordPress_API/Posts#wp.newPost
I0 h$ [ {/ T. r) I" z7 \
% x( u! S, y1 t/ J: b
其中,blog_id, username, password是每次調用api都需要傳入的身份信息。如果不是mu版本,blog_id使用0。struct content部分是針對每個操作的具體的參數。不過不是每個參數都必須要傳入。& d' S3 E1 Y( k0 ~! {
$ y* Q% m9 D1 y/ d定義api調用接口:' q; M, f& m3 e/ I& s0 M& P
調用我們使用的xmlrpc類庫,需要建立一個從IXmlRpcProxy繼承來的interface,我們這裡取名叫IWordpress,然後把調用的每個api操作的method寫出來,因為此類庫使用了反射,所以參數的名字必須要和文檔裡面一樣。api的名字使用屬性標識,例如:[XmlRpcMethod("wp.newPost")],這樣你interface裡面的方法名稱可以取一個可讀性高一點的名字。- public interface IWordpress : IXmlRpcProxy' \3 x: X& E# c: g; c
- {
# |1 w- \: V8 W5 o - [XmlRpcMethod("wp.newPost")]
' a1 D' }, k7 Q- h. S% F - string NewPost(int blog_id, string username, string password, NewPostInput newPostInput);3 V' D" D! D; O6 o
- }
複製代碼 建立一個方法方便程序裡面調用:- public static string NewPost(string url, string username, string password, string title, string content)
! m' k, G: Q% i' l9 o/ o - {
+ D k1 H: e2 f5 l; O5 e2 { - IWordpress proxy = (IWordpress)XmlRpcProxyGen.Create(typeof(IWordpress)); Y2 \* l- m9 j: b! l
- proxy.Url = url;0 G7 L7 B! C3 ^" Y
- NewPostInput postInput;- \6 {& H; @% h- [
- postInput.post_title = title;
2 Q5 ?. c/ c! } - postInput.post_content = content;, z4 r+ R ?& T- `! d: I
- postInput.post_status = "publish";
8 F5 o* H2 y/ l. r5 F - 7 h N. k' C( X# B* P. N p5 H
- string postId = proxy.NewPost(0, username, password, postInput);9 S2 }; L1 Q" s/ Q0 ^
- return postId;
' @* K [. \+ ]6 w$ Y - }
複製代碼 這裡我們首先通過XmlRpcProxyGen.Create創建了一個剛才IWordpress接口的對象,然後指定wordpress url和傳入參數。
4 V1 h5 m# s$ x6 g" ~/ ~其中,作為Post內容的參數是個復合類型,所以我建立了一個struct來表示:- public struct NewPostInput
& j" ~1 r1 L/ L - {7 z) R7 v0 R& Q
- public string post_title;
9 O' J/ c7 }4 A6 i* A& q - public string post_content;! Q6 W$ a; D N: w0 K# @! k" Z
- public string post_status;1 N' r2 ?1 Y t4 m; V
- }
複製代碼 文檔裡面的很多參數是可選的,所以作為例子,這裡只有3個主要參數。注意,參數的名字要和文檔裡面的一樣。
$ R4 H( ?4 K3 j! K, u
: q- g, N4 a/ b+ e8 w) \如何獲取Post列表? v: W* k: S3 l0 e
同NewPost一樣,先查看文檔,然後把api的定義寫到IWordpress裡面:- public interface IWordpress : IXmlRpcProxy
1 k4 o; `% ?, v# j R - {5 a8 M; L+ V% T8 E. E' S) Z8 {
- [XmlRpcMethod("wp.newPost")]) u7 O) {0 Z2 _: V1 X( d3 Y
- string NewPost(int blog_id, string username, string password, NewPostInput newPostInput);: A* T2 A9 z) b( d) b! H
- 7 W' W0 t1 K: G* v
- [XmlRpcMethod("wp.getPosts")]
7 K3 W- g* S# `( E9 [ - XmlRpcStruct[] GetPosts(int blog_id, string username, string password);9 ?1 h; w/ B/ ]; {. L( u: ^
- }
複製代碼 接下來,建立一個方便用戶調用的方法,GetPosts:- public static XmlRpcStruct[] GetPosts(string url, string username, string password)
' H# c% q9 x% L/ F - {: }) u- ~5 M% e! d0 q1 G( a
- IWordpress wordpress = (IWordpress)XmlRpcProxyGen.Create(typeof(IWordpress));
9 A0 R3 {9 ?% ^5 ], g* l. u - wordpress.Url = url;" t: r1 H+ o; P& _7 }3 B' R+ k
- XmlRpcStruct[] ret = wordpress.GetPosts(0, username, password);6 o: r5 V0 I9 @2 w x
- return ret;, ` M0 N2 L# M
- }
複製代碼 這裡返回的是XmlRpcStruct數組。XmlRpcStruct是可以通過字符串索引內容的,比如:ret[0]["post_title"],可以查詢的字段名稱在文檔的return values裡面有說明:9 h# f+ O- ^6 B3 D
http://codex.wordpress.org/XML-RPC_WordPress_API/Posts#wp.getPost & j2 ]* v+ C- [ ] |# T5 s. a
( s. C, ]8 M6 ^5 x0 h' U, o7 |4 }
調用wordpress api的url是什麼?
: y; _" @2 C% g$ {( a# y' owordpress的安裝目錄下面有個xmlrpc.php,調用的url就是這個文件的web地址,比如:
, |* m3 V% U$ L, z3 K4 M& q/ Xhttp://www.example.com/xmlrpc.php$ \* I4 L- O1 g8 k. M1 j2 c
7 i L3 D& \3 m: K, ~% r, P* `' ~很少寫東西,一動手才感覺簡單的東西,自己雖然知道,但是表達出來挺彆扭。(由己知彼,對寫出一大堆教程的moon light更加佩服了 )。$ R- o: i8 Y F. @1 A
希望能對大家有幫助,為有這方面需要的朋友節約一點時間,附上源代碼供大家動手試試:
% Z% W! f1 O# F6 w
WordpressExample.rar
(50.22 KB, 下載次數: 7)
+ p- V* [6 u4 h0 e/ s
+ k, h, p m5 X6 l, L' ~; s# M) w) B" n: _4 K% \8 a: l
3 ?1 ]2 u8 w' f. j
" Y- F+ Q3 [; D$ G9 ?8 Q$ a K
0 ~- G) X! k6 g7 f. q9 G; T; G9 d3 e! m! U$ V, N. s
|