本帖最後由 hudba 於 2013-6-17 02:45 編輯
9 b' ^2 z0 L- Y( ^$ E4 z+ H5 K, p* Q2 M& @7 @* x
以前用到程序中要調用wordpress api的地方,探索了一下,這裡整理一下發給大家,但願對有需要的朋友有幫助。
3 ?% @; k* {; D9 `1 }- q8 e: ]3 R: { q f5 _
準備工作:
% X4 p0 E# S: n; n4 v使用C#調用,推薦vs2010,這裡有下載:
& c/ p) E" d! v1 ~- fhttp://www.microsoft.com/en-us/download/details.aspx?id=12187 $ U* Q& Q7 ?: ]$ J
程序和wordpress通信需要使用xmlrpc,需要從這裡下載獲得,(或者從我附帶的壓縮包裡也可以獲得):$ t, z) Y/ J* K: N; m
http://xml-rpc.net/download.html
$ Y( P/ E e! e7 ewordpress api的文檔:% J. _5 v( r# W: {( D
http://codex.wordpress.org/XML-RPC_WordPress_API M1 s7 N- E0 s! ?# S7 t( U
要點講述:: s1 n$ J8 M* X) U
vs裡面建立solution:WordpressExample,然後引用xmlrpc的dll,如下圖:
' O- B# n4 R% e( U
6 A9 L$ y" A9 t+ X5 H5 t4 _8 W% A
# _: j( s. W _" [0 W
如何新建Post?# \- j* @& G: g( u# m) K
查看wordpress的文檔,找到newPost操作需要傳入的參數:
2 k+ ?! `$ J" uhttp://codex.wordpress.org/XML-RPC_WordPress_API/Posts#wp.newPost
) F7 X2 W/ R' b; i4 n; a0 P
( M) _& n6 V" d# B, i o
其中,blog_id, username, password是每次調用api都需要傳入的身份信息。如果不是mu版本,blog_id使用0。struct content部分是針對每個操作的具體的參數。不過不是每個參數都必須要傳入。
8 p8 S7 u( @ F5 p' T' I7 t c# U4 |) s# c# L
定義api調用接口:+ J- p' _6 N r; T2 o- {; D0 w
調用我們使用的xmlrpc類庫,需要建立一個從IXmlRpcProxy繼承來的interface,我們這裡取名叫IWordpress,然後把調用的每個api操作的method寫出來,因為此類庫使用了反射,所以參數的名字必須要和文檔裡面一樣。api的名字使用屬性標識,例如:[XmlRpcMethod("wp.newPost")],這樣你interface裡面的方法名稱可以取一個可讀性高一點的名字。- public interface IWordpress : IXmlRpcProxy* {4 W3 [) F, l
- {2 B. O9 A$ {0 }. Q5 _
- [XmlRpcMethod("wp.newPost")]
; F0 e+ k/ ]* P - string NewPost(int blog_id, string username, string password, NewPostInput newPostInput);* b; X C% H; V1 s( m9 }
- }
複製代碼 建立一個方法方便程序裡面調用:- public static string NewPost(string url, string username, string password, string title, string content)! C. \( ]0 o9 R. }
- {
2 u% _/ P4 d' M1 {1 Q - IWordpress proxy = (IWordpress)XmlRpcProxyGen.Create(typeof(IWordpress));
) Z9 f# R; ] q, L, h - proxy.Url = url;: C b" X* B7 w, |
- NewPostInput postInput;& y9 n8 [5 |2 E% n( G
- postInput.post_title = title;
9 J5 I! J: x: A - postInput.post_content = content;( X: c% c; E# V* f! F& L
- postInput.post_status = "publish";
$ n$ S; N8 H% A( ]. f# s* \ - 4 P* M& H7 t8 v# D% m7 G: \
- string postId = proxy.NewPost(0, username, password, postInput);* u) T% k) K: T3 W3 N* L4 Y
- return postId;
) P; q. H% L4 J5 m$ A# ~ - }
複製代碼 這裡我們首先通過XmlRpcProxyGen.Create創建了一個剛才IWordpress接口的對象,然後指定wordpress url和傳入參數。3 s N* n( H. Z; x
其中,作為Post內容的參數是個復合類型,所以我建立了一個struct來表示:- public struct NewPostInput
9 S& B3 ~: P3 N( Z1 f1 e4 r9 a5 v - {
4 U( E3 n. ~, z! I - public string post_title;
! y: [6 I0 r! l9 O: N. Y7 G - public string post_content;
% E) P. a) t+ H - public string post_status;
* r4 ~ D& q7 o0 l& k - }
複製代碼 文檔裡面的很多參數是可選的,所以作為例子,這裡只有3個主要參數。注意,參數的名字要和文檔裡面的一樣。5 q( v; o8 |5 C! N! f
* ]! p; ], C: w v* h E3 I0 `8 h
如何獲取Post列表? s" g0 h# r3 D& P
同NewPost一樣,先查看文檔,然後把api的定義寫到IWordpress裡面:- public interface IWordpress : IXmlRpcProxy+ g, i% k. ^. f; y, E) V( Q
- {
* t" b- c1 `9 A+ D# t F! H - [XmlRpcMethod("wp.newPost")]1 g/ Z0 l& j6 d- i- v
- string NewPost(int blog_id, string username, string password, NewPostInput newPostInput);) v4 P! D+ F) T* ^2 n2 S2 O7 _
& ]2 I: a9 t) a6 o0 Y7 k- [XmlRpcMethod("wp.getPosts")]9 G+ X q" |$ i) B
- XmlRpcStruct[] GetPosts(int blog_id, string username, string password);! ~* D& T j* u3 w
- }
複製代碼 接下來,建立一個方便用戶調用的方法,GetPosts:- public static XmlRpcStruct[] GetPosts(string url, string username, string password): D r9 Z+ {" L1 @& u8 K2 a) _
- {
$ n1 |6 K1 q) M( Y7 V - IWordpress wordpress = (IWordpress)XmlRpcProxyGen.Create(typeof(IWordpress));
7 u5 f8 {& g7 `! k4 u- z - wordpress.Url = url;/ o' j9 h) `& s
- XmlRpcStruct[] ret = wordpress.GetPosts(0, username, password);2 d& Y; [4 q8 ?4 v/ k) d5 P' F
- return ret;
: `: ~+ j6 ^" S& k- R. v {) F - }
複製代碼 這裡返回的是XmlRpcStruct數組。XmlRpcStruct是可以通過字符串索引內容的,比如:ret[0]["post_title"],可以查詢的字段名稱在文檔的return values裡面有說明:( U/ p+ O# o- Q; e
http://codex.wordpress.org/XML-RPC_WordPress_API/Posts#wp.getPost 3 z$ a5 r( G$ {0 g& c
& a J: ^1 r o9 z9 b2 A; I/ i
6 h, _' }* b' V1 ^6 S
調用wordpress api的url是什麼?) k+ _! n+ i' t: B( a' e! f! I
wordpress的安裝目錄下面有個xmlrpc.php,調用的url就是這個文件的web地址,比如:" S3 r* {: b+ @3 N+ [
http://www.example.com/xmlrpc.php H, |. ~$ m0 x- k$ N
3 @1 \0 x7 ] t) Z) M( \很少寫東西,一動手才感覺簡單的東西,自己雖然知道,但是表達出來挺彆扭。(由己知彼,對寫出一大堆教程的moon light更加佩服了 )。, i3 ?& H2 i* A1 u1 ^( ]
希望能對大家有幫助,為有這方面需要的朋友節約一點時間,附上源代碼供大家動手試試:7 U' a9 \) u/ w" f4 R, S/ O- N
WordpressExample.rar
(50.22 KB, 下載次數: 7)
- x( x) \3 ?5 b4 k8 @, }
& B- p. B! k$ l3 f! e6 V9 o0 k8 p, _4 k' ^
: s$ v3 t. L3 t6 y
' Z( K0 q4 M3 d* A8 u# i. y3 Y- G$ ]4 F# L
8 F8 `/ D) T& f2 v! S8 X+ z, b% |
% ]- U; n% ~" ^) w7 S |