本帖最後由 hudba 於 2013-6-17 02:45 編輯
5 D9 Y2 K; x7 E' Q$ B; Q+ ?
; x/ S/ B; L% t; f2 a以前用到程序中要調用wordpress api的地方,探索了一下,這裡整理一下發給大家,但願對有需要的朋友有幫助。
* W) L) c: E/ M/ }- ^ K9 I& T8 O. C
% L R6 s) }. w% z2 p8 m準備工作:9 z- o0 ?* M( T) h8 a
使用C#調用,推薦vs2010,這裡有下載:
& f0 e% j/ j$ ?1 d- L, d: thttp://www.microsoft.com/en-us/download/details.aspx?id=12187 ; u5 @8 ]3 H4 f* c7 c( N& ` _; u
程序和wordpress通信需要使用xmlrpc,需要從這裡下載獲得,(或者從我附帶的壓縮包裡也可以獲得):
* z0 \8 Z- x+ L* ?http://xml-rpc.net/download.html
" Z7 t# G1 ~! Z9 ]: R ~wordpress api的文檔:
^; T0 M% S/ |1 r2 U2 Rhttp://codex.wordpress.org/XML-RPC_WordPress_API 2 u# b+ x+ w2 }2 S* [/ p; O
要點講述:+ i: d( M3 l9 H' o
vs裡面建立solution:WordpressExample,然後引用xmlrpc的dll,如下圖:
; i6 b( A' s; `9 V7 {0 P1 e7 @, }: @, ^
$ E) K A4 K% t) ?
; v/ p4 a9 N6 t如何新建Post?! _6 |8 b0 \4 V/ V
查看wordpress的文檔,找到newPost操作需要傳入的參數:
; U6 v O) D8 j/ ~1 V$ qhttp://codex.wordpress.org/XML-RPC_WordPress_API/Posts#wp.newPost 1 ?& a W0 y7 D9 Z
3 g7 s3 r8 V) }* k
其中,blog_id, username, password是每次調用api都需要傳入的身份信息。如果不是mu版本,blog_id使用0。struct content部分是針對每個操作的具體的參數。不過不是每個參數都必須要傳入。
8 q& }4 F8 N4 s8 \/ s" n+ N. v, D2 K' Z" B
定義api調用接口:- x! J: ?* f4 a6 E3 W* r
調用我們使用的xmlrpc類庫,需要建立一個從IXmlRpcProxy繼承來的interface,我們這裡取名叫IWordpress,然後把調用的每個api操作的method寫出來,因為此類庫使用了反射,所以參數的名字必須要和文檔裡面一樣。api的名字使用屬性標識,例如:[XmlRpcMethod("wp.newPost")],這樣你interface裡面的方法名稱可以取一個可讀性高一點的名字。- public interface IWordpress : IXmlRpcProxy
/ @% q$ a- f8 b( W - {3 H8 A# w* b( d$ P# e9 I3 M7 S
- [XmlRpcMethod("wp.newPost")]
- D3 _3 o5 h9 q4 P. N9 O - string NewPost(int blog_id, string username, string password, NewPostInput newPostInput);
. x' `% I9 |4 ]7 A( y7 F) d2 J3 Q - }
複製代碼 建立一個方法方便程序裡面調用:- public static string NewPost(string url, string username, string password, string title, string content)
3 v, Q% [9 ?7 k3 D; x& Z; D, F - {1 V6 E. j; P; [, I
- IWordpress proxy = (IWordpress)XmlRpcProxyGen.Create(typeof(IWordpress));; V$ U: n" z6 `. y7 _8 S, h5 L
- proxy.Url = url;
, }' m0 ~& c; m+ l L. n# q! B - NewPostInput postInput;
- A, u9 B1 O4 J6 j M% _9 E - postInput.post_title = title;, y3 ?2 k5 T, c% z
- postInput.post_content = content;
) \* R& y5 o5 t: G1 Z2 E$ t. I - postInput.post_status = "publish";
/ `( B; N, x7 C* ]
. x+ p* n3 _; V/ ?% \ r- string postId = proxy.NewPost(0, username, password, postInput);
/ |0 w( n* U4 k% K4 k - return postId;
6 V+ ^* u! Q, R - }
複製代碼 這裡我們首先通過XmlRpcProxyGen.Create創建了一個剛才IWordpress接口的對象,然後指定wordpress url和傳入參數。
7 g! T! h- {* M6 j, q P其中,作為Post內容的參數是個復合類型,所以我建立了一個struct來表示:- public struct NewPostInput
0 `- ?) e$ X" C v+ k' m - {) M N, M `- {4 C! \6 ~' q6 m n
- public string post_title;
! c! b# b% m1 T7 V3 J. x8 @ - public string post_content;$ p0 F, L% N( A+ E; S# S
- public string post_status;3 d" V, @. {! L7 P
- }
複製代碼 文檔裡面的很多參數是可選的,所以作為例子,這裡只有3個主要參數。注意,參數的名字要和文檔裡面的一樣。
/ g. I5 Z6 n- M x, w! e, k: B, e# T4 \# h. c& x# R7 V1 ~. u/ O; d
如何獲取Post列表? P/ P6 p) Y$ v8 K. C% p: Z1 L' h
同NewPost一樣,先查看文檔,然後把api的定義寫到IWordpress裡面:- public interface IWordpress : IXmlRpcProxy
! `. ~# z# l) R/ I - {
4 @) Z9 c4 N: M5 |. M9 K - [XmlRpcMethod("wp.newPost")]8 r* T+ O, ]. L* S
- string NewPost(int blog_id, string username, string password, NewPostInput newPostInput);/ l$ h* B8 R- u8 M! [; N
- # g! N( m3 W Y3 f3 o7 G/ X' i
- [XmlRpcMethod("wp.getPosts")]. S! o# Y* ]4 q- X# P+ Z
- XmlRpcStruct[] GetPosts(int blog_id, string username, string password);. Y2 P' a& |' L: u4 W
- }
複製代碼 接下來,建立一個方便用戶調用的方法,GetPosts:- public static XmlRpcStruct[] GetPosts(string url, string username, string password)9 S2 a2 C# j( v6 {
- {
7 c6 Q! w U9 @; F2 q- X4 u - IWordpress wordpress = (IWordpress)XmlRpcProxyGen.Create(typeof(IWordpress));1 W( ]' w+ s. v x
- wordpress.Url = url;+ V7 O2 e Y3 l" ?' j5 [
- XmlRpcStruct[] ret = wordpress.GetPosts(0, username, password);
1 ^ |, G5 c0 i# z - return ret;$ w7 O. e& E. ~9 h+ b& T- W$ n
- }
複製代碼 這裡返回的是XmlRpcStruct數組。XmlRpcStruct是可以通過字符串索引內容的,比如:ret[0]["post_title"],可以查詢的字段名稱在文檔的return values裡面有說明:
7 }, V7 ]' S0 n4 khttp://codex.wordpress.org/XML-RPC_WordPress_API/Posts#wp.getPost z. c- H$ k$ m6 A) M4 p9 @
: ^$ h( c; Q% s" B6 g: T7 {
" ?5 R6 h; e l) `6 }' x$ x6 G& P調用wordpress api的url是什麼?
2 _* S; }# ~, x2 N9 Swordpress的安裝目錄下面有個xmlrpc.php,調用的url就是這個文件的web地址,比如:2 w4 ? q& r3 P3 R5 Y4 h9 k2 m2 D
http://www.example.com/xmlrpc.php
2 r. c9 _+ Q+ N; K1 I5 _) c; Y$ r8 y6 }% {( \8 P
很少寫東西,一動手才感覺簡單的東西,自己雖然知道,但是表達出來挺彆扭。(由己知彼,對寫出一大堆教程的moon light更加佩服了 )。
5 |, G2 b/ x2 T希望能對大家有幫助,為有這方面需要的朋友節約一點時間,附上源代碼供大家動手試試:
0 G2 o/ k( E! ]
WordpressExample.rar
(50.22 KB, 下載次數: 7)
) U! N9 [9 E5 ?' L
! }2 I. D+ v: U. o+ g" I \$ I4 G, {+ j: p
9 D! C, Z; w! u: \
7 x/ W! o$ Y) H5 z: Z
W7 {& }# ^9 u ]- b$ J* D# S) ~" M
|