本帖最後由 hudba 於 2013-6-17 02:45 編輯 $ F1 A0 n4 \# f7 r
. p( m! A5 X8 ^. u
以前用到程序中要調用wordpress api的地方,探索了一下,這裡整理一下發給大家,但願對有需要的朋友有幫助。( a0 m- ]& r# K p: H* [8 D9 F2 e
$ F( g3 q' C- A
準備工作:/ D! z8 k; ]/ ?# w) |8 J7 n
使用C#調用,推薦vs2010,這裡有下載:
: }) F) I6 e6 e: J! G& Ghttp://www.microsoft.com/en-us/download/details.aspx?id=12187
- W. \3 {4 k2 I# u& [程序和wordpress通信需要使用xmlrpc,需要從這裡下載獲得,(或者從我附帶的壓縮包裡也可以獲得):: b4 t3 `1 {9 ^6 g* f9 x) k
http://xml-rpc.net/download.html
; T: t$ ^% X* L5 p( s+ N1 |wordpress api的文檔:' G l# f/ h$ s& {; h
http://codex.wordpress.org/XML-RPC_WordPress_API $ s ^# n( X" k) j
要點講述:
# j0 o$ m4 w4 Ovs裡面建立solution:WordpressExample,然後引用xmlrpc的dll,如下圖:
. N! q) O2 ], S+ B
5 l3 Q4 i: a; C9 A( H$ `/ k. } ]: b* D: S, X
如何新建Post?6 @5 q1 f D, t2 ~ N' S
查看wordpress的文檔,找到newPost操作需要傳入的參數:& j' M# ]3 C0 W3 r7 f, B- z
http://codex.wordpress.org/XML-RPC_WordPress_API/Posts#wp.newPost 8 C+ Z v# B& l4 ~2 U
: {; j7 \$ q* Q* K, r其中,blog_id, username, password是每次調用api都需要傳入的身份信息。如果不是mu版本,blog_id使用0。struct content部分是針對每個操作的具體的參數。不過不是每個參數都必須要傳入。
0 K1 e2 h2 E9 V# V0 W1 \5 t P! P- C& A% c' E* h7 p! ~
定義api調用接口:7 U& C0 m6 ~9 b
調用我們使用的xmlrpc類庫,需要建立一個從IXmlRpcProxy繼承來的interface,我們這裡取名叫IWordpress,然後把調用的每個api操作的method寫出來,因為此類庫使用了反射,所以參數的名字必須要和文檔裡面一樣。api的名字使用屬性標識,例如:[XmlRpcMethod("wp.newPost")],這樣你interface裡面的方法名稱可以取一個可讀性高一點的名字。- public interface IWordpress : IXmlRpcProxy/ s0 ~1 \9 s& b# {7 V% V7 G# M
- {( @2 G w' O' \ Y9 Q1 C
- [XmlRpcMethod("wp.newPost")]. z6 z- B& q9 R! Z( Y
- string NewPost(int blog_id, string username, string password, NewPostInput newPostInput);
1 d& [/ [. t# C) f6 w - }
複製代碼 建立一個方法方便程序裡面調用:- public static string NewPost(string url, string username, string password, string title, string content)( I* B% z# C: n( ]* Z& ?
- {" G1 @" w* m/ n0 O
- IWordpress proxy = (IWordpress)XmlRpcProxyGen.Create(typeof(IWordpress));( q- W2 U3 o5 n! x6 h2 B& F- ^
- proxy.Url = url;
8 `- C6 `) Q$ C7 [) m - NewPostInput postInput;6 n! ~: z. W0 U1 ?- ^2 O
- postInput.post_title = title;2 b% ~* w+ x- ~8 @" S2 S+ N
- postInput.post_content = content;
% a9 \6 j* x- { - postInput.post_status = "publish";
. |. x% l2 F/ X
$ ^2 E) _- }$ U2 ?+ u- string postId = proxy.NewPost(0, username, password, postInput);2 ?4 y' ?. R" [& ?) g. A
- return postId;
5 i8 n. @5 e! S6 N! C/ x - }
複製代碼 這裡我們首先通過XmlRpcProxyGen.Create創建了一個剛才IWordpress接口的對象,然後指定wordpress url和傳入參數。, t( V: v$ A3 p! |
其中,作為Post內容的參數是個復合類型,所以我建立了一個struct來表示:- public struct NewPostInput# U) v0 t5 I: u; v" Z4 y, q+ v+ [6 ~% Y
- {
7 m+ f9 t: a" A: J) \ - public string post_title;, M' {4 b8 B$ Y7 j6 \; h9 B# R+ d5 f1 [
- public string post_content;
* g# @% {, L- { - public string post_status;) G3 M9 Z* C% `6 J. n p
- }
複製代碼 文檔裡面的很多參數是可選的,所以作為例子,這裡只有3個主要參數。注意,參數的名字要和文檔裡面的一樣。 }" K2 n. |0 |# U/ J4 q' @
/ t- t8 \9 g u+ c; j
如何獲取Post列表?3 a4 l' G! V8 I! C8 E2 l
同NewPost一樣,先查看文檔,然後把api的定義寫到IWordpress裡面:- public interface IWordpress : IXmlRpcProxy
$ q& R5 a9 e# P; i' q - {
" l e" |1 B9 u - [XmlRpcMethod("wp.newPost")]
) [1 s. N7 K, R: R9 H3 ? - string NewPost(int blog_id, string username, string password, NewPostInput newPostInput);- p0 P9 @! O4 g: R, W. D
- 6 q4 C" K$ F, d! p) R0 |# Q# e. t
- [XmlRpcMethod("wp.getPosts")]
4 Q7 S. @0 l+ B/ Y U2 V - XmlRpcStruct[] GetPosts(int blog_id, string username, string password);
. Q1 [) C" O1 i2 `7 A1 t - }
複製代碼 接下來,建立一個方便用戶調用的方法,GetPosts:- public static XmlRpcStruct[] GetPosts(string url, string username, string password)
W3 H4 l' W2 p# H u1 L - {. J% N) R5 Z2 W4 A. \( u
- IWordpress wordpress = (IWordpress)XmlRpcProxyGen.Create(typeof(IWordpress));
8 J" @3 @9 X" u! D* K7 K+ q1 _8 Q - wordpress.Url = url;9 `2 `) [& L& {
- XmlRpcStruct[] ret = wordpress.GetPosts(0, username, password);; D9 e* |: R- f" ?" o1 }7 i1 j2 `
- return ret; a7 l& m$ S7 q' Q* J: e
- }
複製代碼 這裡返回的是XmlRpcStruct數組。XmlRpcStruct是可以通過字符串索引內容的,比如:ret[0]["post_title"],可以查詢的字段名稱在文檔的return values裡面有說明:
% m/ G) M8 Y$ y/ ghttp://codex.wordpress.org/XML-RPC_WordPress_API/Posts#wp.getPost 0 {0 Q& I1 \/ W2 B+ _2 o- U
$ o3 d7 \' N! R! T7 G2 u" ]
8 W# |" d. a5 r% l調用wordpress api的url是什麼?
# c; g# l5 E3 | s! \" a/ V5 z7 gwordpress的安裝目錄下面有個xmlrpc.php,調用的url就是這個文件的web地址,比如:# u& ~0 m9 b! g2 N" |* b4 R* e
http://www.example.com/xmlrpc.php+ N8 _# R5 S0 n; H) x% L0 }
* x( Y) H9 q4 z. g) v
很少寫東西,一動手才感覺簡單的東西,自己雖然知道,但是表達出來挺彆扭。(由己知彼,對寫出一大堆教程的moon light更加佩服了 )。' [* }% j$ ?% W- c
希望能對大家有幫助,為有這方面需要的朋友節約一點時間,附上源代碼供大家動手試試:, u5 a0 O! l" b
WordpressExample.rar
(50.22 KB, 下載次數: 7)
' B0 {3 \- v* J, y3 O( o3 L
3 j! @" j8 u: w6 O
. b9 P( N$ d% O1 r: ?: H" m
( j: O2 A6 S! ^$ x X+ p0 |' `$ P( Y7 ^% c; o) [3 h* e
2 {# ~9 o, N5 i+ E4 z4 c5 m
' u; M2 r) X- u; z, \& i
|