本帖最後由 hudba 於 2013-6-17 02:45 編輯 3 S w9 |! V/ f1 J
% ?# w" n5 H) O. [" A! p* U6 x# X+ y) t
以前用到程序中要調用wordpress api的地方,探索了一下,這裡整理一下發給大家,但願對有需要的朋友有幫助。# [0 Q j+ h n. }6 p
. r1 f8 |$ P' l7 i- A準備工作:
+ H' x) L. j y. s. |使用C#調用,推薦vs2010,這裡有下載:; d9 z* b% }6 ~4 a
http://www.microsoft.com/en-us/download/details.aspx?id=12187 $ X1 `$ ^$ c" t# u/ Z0 y i
程序和wordpress通信需要使用xmlrpc,需要從這裡下載獲得,(或者從我附帶的壓縮包裡也可以獲得):' H; ~+ l# l; k( D6 V
http://xml-rpc.net/download.html
/ p/ V1 F& g9 x3 p2 Ywordpress api的文檔:
3 d& D8 V2 y. E2 G- } ?http://codex.wordpress.org/XML-RPC_WordPress_API
$ S' s9 O6 v* p要點講述:
: M( `& r3 S7 H* j( T! w8 b0 k! ^1 Xvs裡面建立solution:WordpressExample,然後引用xmlrpc的dll,如下圖:! K# S8 s& U% u5 ]+ [, B- a3 A
, v; ~2 H% d3 m( v: Q
+ V5 n( ], J- b9 K如何新建Post?
# S' s; W# r$ w- p$ @5 \查看wordpress的文檔,找到newPost操作需要傳入的參數:
; V" A& a, S1 y3 q( _# ?6 }( @http://codex.wordpress.org/XML-RPC_WordPress_API/Posts#wp.newPost
! ]+ q7 }$ Y/ e& X
3 j5 b% Y, V1 v& I3 u' q其中,blog_id, username, password是每次調用api都需要傳入的身份信息。如果不是mu版本,blog_id使用0。struct content部分是針對每個操作的具體的參數。不過不是每個參數都必須要傳入。
7 ?; C4 a: l" a' x* F5 E
`: v) K- p, t ~定義api調用接口:. |5 F% A/ v u% H3 {
調用我們使用的xmlrpc類庫,需要建立一個從IXmlRpcProxy繼承來的interface,我們這裡取名叫IWordpress,然後把調用的每個api操作的method寫出來,因為此類庫使用了反射,所以參數的名字必須要和文檔裡面一樣。api的名字使用屬性標識,例如:[XmlRpcMethod("wp.newPost")],這樣你interface裡面的方法名稱可以取一個可讀性高一點的名字。- public interface IWordpress : IXmlRpcProxy4 F6 [. F/ w* z
- {* i7 Q7 `5 m: c
- [XmlRpcMethod("wp.newPost")]
2 u2 v' [) ]# p/ \! `- m" s: s - string NewPost(int blog_id, string username, string password, NewPostInput newPostInput);1 s/ l( J. m' Y
- }
複製代碼 建立一個方法方便程序裡面調用:- public static string NewPost(string url, string username, string password, string title, string content)1 d" S$ o* ]) }# U; O
- {
% S; r" |$ O6 D3 ?6 F: { - IWordpress proxy = (IWordpress)XmlRpcProxyGen.Create(typeof(IWordpress));
7 ^1 _) \8 n6 e6 B" Y) J - proxy.Url = url;7 ~4 Q$ K- h5 f7 P& t" `$ ^% B% o
- NewPostInput postInput;
& m$ z T T# B - postInput.post_title = title;
3 S) b5 h/ E; x- P$ m! r - postInput.post_content = content;
1 U' \1 B; n Q - postInput.post_status = "publish";
8 ~+ Q7 u0 ?. D% z - - M. C& b. I0 I9 y
- string postId = proxy.NewPost(0, username, password, postInput);/ N! [. n H6 |! l4 J
- return postId;& U) ^1 Z$ p" r/ d
- }
複製代碼 這裡我們首先通過XmlRpcProxyGen.Create創建了一個剛才IWordpress接口的對象,然後指定wordpress url和傳入參數。
9 Z8 D. b6 s7 f( b( G1 J* n9 j其中,作為Post內容的參數是個復合類型,所以我建立了一個struct來表示:- public struct NewPostInput
! A" A8 b N0 R9 S( b; N - {
; q. a: i1 L5 N# O t - public string post_title;$ J, {/ E$ V: b% I! W7 f ~
- public string post_content;( n" H0 P8 U* Y. l X$ B* n; @' P
- public string post_status;0 }- S1 N1 G2 ^2 I2 ]6 A; P
- }
複製代碼 文檔裡面的很多參數是可選的,所以作為例子,這裡只有3個主要參數。注意,參數的名字要和文檔裡面的一樣。
5 y- s" @- M; n' q8 m
2 N: k& [* P, G% j9 e3 \ D6 n x" C如何獲取Post列表?6 n3 w( [9 F* }) Y
同NewPost一樣,先查看文檔,然後把api的定義寫到IWordpress裡面:- public interface IWordpress : IXmlRpcProxy+ L: P/ I1 u# o [" `7 P# Q, r
- {- G3 V; D4 z" f7 g6 G
- [XmlRpcMethod("wp.newPost")]
' |" R: ^; X$ T* S" _$ a! ^ - string NewPost(int blog_id, string username, string password, NewPostInput newPostInput);' P' O4 N0 x2 |, a
- 4 {6 ~* E0 v0 `# y
- [XmlRpcMethod("wp.getPosts")]0 T/ I @7 q$ O# l% G) Q
- XmlRpcStruct[] GetPosts(int blog_id, string username, string password);0 t0 H' q/ V: W
- }
複製代碼 接下來,建立一個方便用戶調用的方法,GetPosts:- public static XmlRpcStruct[] GetPosts(string url, string username, string password)1 E, I8 Y* c% Y( D: r7 g% w
- {
6 _* F$ N3 K! c6 Y* z. N2 Z+ E - IWordpress wordpress = (IWordpress)XmlRpcProxyGen.Create(typeof(IWordpress));
. F) N2 `$ F# x' _6 ` - wordpress.Url = url;
: V* M9 J8 ?# G9 o - XmlRpcStruct[] ret = wordpress.GetPosts(0, username, password);9 }* f, X* Z& G' i v3 J2 `' e$ X. |
- return ret;9 f3 E+ |1 J. L0 B% m" A
- }
複製代碼 這裡返回的是XmlRpcStruct數組。XmlRpcStruct是可以通過字符串索引內容的,比如:ret[0]["post_title"],可以查詢的字段名稱在文檔的return values裡面有說明:
% T b) w E; S; h. w7 Qhttp://codex.wordpress.org/XML-RPC_WordPress_API/Posts#wp.getPost / F* D) s/ @! U/ v
' s7 L/ o8 b, U2 S3 P0 R' w( K7 ?4 S$ K% o+ X5 P& y- A
調用wordpress api的url是什麼?
# @/ ^2 O( |" u8 M4 j5 Swordpress的安裝目錄下面有個xmlrpc.php,調用的url就是這個文件的web地址,比如:
, j9 V) O- _! R4 I# `+ p2 n! P" khttp://www.example.com/xmlrpc.php/ i! ?* ^: [. w9 v2 e4 W
) `6 X) g, @+ K& k0 X很少寫東西,一動手才感覺簡單的東西,自己雖然知道,但是表達出來挺彆扭。(由己知彼,對寫出一大堆教程的moon light更加佩服了 )。
6 S8 F, U/ @% n希望能對大家有幫助,為有這方面需要的朋友節約一點時間,附上源代碼供大家動手試試:/ K' x# D1 K% \1 N* p2 P
WordpressExample.rar
(50.22 KB, 下載次數: 7)
& z6 A$ y x4 C+ P1 V
8 N$ z$ N0 c3 J5 I3 H
1 F) d: a4 p @2 h/ J& C" K# S7 t. \4 O- Y ] }# L6 O
+ A. e7 [1 F# }) Z/ Y
5 g2 G5 ?! A. l$ e- L
3 u% @* q5 d. z: I& Z/ J% Y- c |