本帖最後由 hudba 於 2013-6-17 02:45 編輯 2 E+ D$ z8 O, t1 H* F, l O: \
+ q2 B" E) B. A( `# k
以前用到程序中要調用wordpress api的地方,探索了一下,這裡整理一下發給大家,但願對有需要的朋友有幫助。5 I2 b8 Y: y- r/ y$ g" x
5 ?" n8 B! C- S6 o( [
準備工作:
! A. F R: _; ?& g6 v/ z使用C#調用,推薦vs2010,這裡有下載:
6 D8 z$ G; r3 \! q$ H+ H# Z+ {$ W* Bhttp://www.microsoft.com/en-us/download/details.aspx?id=12187
: y9 F+ ]( ~8 |/ r$ B0 @3 o程序和wordpress通信需要使用xmlrpc,需要從這裡下載獲得,(或者從我附帶的壓縮包裡也可以獲得):. k' }4 i) U" t
http://xml-rpc.net/download.html 6 X% ?# I6 m& j _+ h- t
wordpress api的文檔:
- O# R i5 ]& t' ~/ u& E# Ghttp://codex.wordpress.org/XML-RPC_WordPress_API . s! R$ e& V) u- @
要點講述:9 Z7 `) R+ j% V$ o/ n
vs裡面建立solution:WordpressExample,然後引用xmlrpc的dll,如下圖:% a& C8 u: ]+ O
6 T; Q& w5 K8 T3 U# U* z
0 y5 J. c; |9 e1 L& H如何新建Post?) A: \) N* {, I% h3 D
查看wordpress的文檔,找到newPost操作需要傳入的參數:
$ \& I$ l: }; u5 R. whttp://codex.wordpress.org/XML-RPC_WordPress_API/Posts#wp.newPost 0 m# m) B( A! t. q) y+ N* q' e6 c
, x6 J D) C) Y其中,blog_id, username, password是每次調用api都需要傳入的身份信息。如果不是mu版本,blog_id使用0。struct content部分是針對每個操作的具體的參數。不過不是每個參數都必須要傳入。$ F; q9 ^8 D9 {8 `
& O; s; G8 J7 t& X, M$ \4 ]# U定義api調用接口:1 ?/ g% `" Z3 f. i4 s6 G) e
調用我們使用的xmlrpc類庫,需要建立一個從IXmlRpcProxy繼承來的interface,我們這裡取名叫IWordpress,然後把調用的每個api操作的method寫出來,因為此類庫使用了反射,所以參數的名字必須要和文檔裡面一樣。api的名字使用屬性標識,例如:[XmlRpcMethod("wp.newPost")],這樣你interface裡面的方法名稱可以取一個可讀性高一點的名字。- public interface IWordpress : IXmlRpcProxy6 m9 {* j+ R( ?0 x, l8 a m
- {
) ~" s; k8 P, O1 ]" p - [XmlRpcMethod("wp.newPost")]
" {& a8 {) i* M9 E5 a1 r - string NewPost(int blog_id, string username, string password, NewPostInput newPostInput);
) s; y$ J4 D& Y5 L* z9 H - }
複製代碼 建立一個方法方便程序裡面調用:- public static string NewPost(string url, string username, string password, string title, string content)
: J' r1 Z: A. l: v3 [2 P - {& }+ o+ ?! h$ n- Y0 z# e" @
- IWordpress proxy = (IWordpress)XmlRpcProxyGen.Create(typeof(IWordpress));
4 \/ F; T& v5 R# i" O - proxy.Url = url;5 Z4 ?2 U5 \7 n. [! K& \3 J T
- NewPostInput postInput;# I9 o" P% w' w6 X+ s* c
- postInput.post_title = title;, }( W* a0 j4 {" O6 N g
- postInput.post_content = content;- V7 g8 T$ e3 X$ _) s3 ^6 ]4 }
- postInput.post_status = "publish";/ M- H- g% x F
4 q5 V, a$ |! {. o6 w8 \- string postId = proxy.NewPost(0, username, password, postInput);+ T1 _8 y6 z( }2 U0 w; ~* i) \
- return postId;
: E9 X& {" i% h% ]9 `. q - }
複製代碼 這裡我們首先通過XmlRpcProxyGen.Create創建了一個剛才IWordpress接口的對象,然後指定wordpress url和傳入參數。) J3 L6 C' u8 y
其中,作為Post內容的參數是個復合類型,所以我建立了一個struct來表示:- public struct NewPostInput+ z1 R: E% c+ n
- {& e0 b% \" `0 w" u! U2 i7 w7 `; Q
- public string post_title;
: g4 a5 I. d2 M4 K. j, X - public string post_content;
# y0 c2 O% k( m; l0 o$ G - public string post_status;
- |) k) t3 s7 F& j# T( V8 w - }
複製代碼 文檔裡面的很多參數是可選的,所以作為例子,這裡只有3個主要參數。注意,參數的名字要和文檔裡面的一樣。5 m3 w; Q/ `3 T3 J6 i
2 y# S! f) Z) l1 h2 A8 _
如何獲取Post列表?
& @" L' u9 n! U/ X: u同NewPost一樣,先查看文檔,然後把api的定義寫到IWordpress裡面:- public interface IWordpress : IXmlRpcProxy
7 y6 i) I' e* C6 X9 B- ?% j% A+ _ - {, l( g( o) e( ^
- [XmlRpcMethod("wp.newPost")], h. j3 V5 H0 Q& }/ `1 P
- string NewPost(int blog_id, string username, string password, NewPostInput newPostInput);
3 n* V/ v6 N1 P2 j& q3 h! \ - ; w1 P; ? n: y! N0 z) v
- [XmlRpcMethod("wp.getPosts")]! r8 i5 ^5 \2 n) O) k3 C/ z
- XmlRpcStruct[] GetPosts(int blog_id, string username, string password);9 u: V; K+ W4 J4 d" Z
- }
複製代碼 接下來,建立一個方便用戶調用的方法,GetPosts:- public static XmlRpcStruct[] GetPosts(string url, string username, string password): {0 e$ v- {2 o7 d: h4 H
- {
( \8 O. S* @! }* P - IWordpress wordpress = (IWordpress)XmlRpcProxyGen.Create(typeof(IWordpress));
/ j1 j# s- w4 @) T# ]/ I( H, U - wordpress.Url = url;/ R; f8 }+ Z6 _. H+ O
- XmlRpcStruct[] ret = wordpress.GetPosts(0, username, password);9 \- Z+ m0 C" h, _. D# y4 c v; I! t
- return ret;
9 a$ N! c- u8 { _& _; {: D - }
複製代碼 這裡返回的是XmlRpcStruct數組。XmlRpcStruct是可以通過字符串索引內容的,比如:ret[0]["post_title"],可以查詢的字段名稱在文檔的return values裡面有說明:
) i' [6 q% b1 F% r ]7 ]- Yhttp://codex.wordpress.org/XML-RPC_WordPress_API/Posts#wp.getPost 4 j v! {0 I6 o" T, a
3 t) _" h( r" m7 D2 l- B. i' y, g5 w1 v5 I. D
調用wordpress api的url是什麼?; _* q c9 F0 r' d, @ H
wordpress的安裝目錄下面有個xmlrpc.php,調用的url就是這個文件的web地址,比如:
: w) u2 w, y9 v# o bhttp://www.example.com/xmlrpc.php2 i# w# D6 k" `' a6 G: r
; ?" w6 ~$ k9 ^# @很少寫東西,一動手才感覺簡單的東西,自己雖然知道,但是表達出來挺彆扭。(由己知彼,對寫出一大堆教程的moon light更加佩服了 )。$ {" n# J$ G, S* P- L
希望能對大家有幫助,為有這方面需要的朋友節約一點時間,附上源代碼供大家動手試試:. d$ O+ i5 v% B2 R f5 J: ^4 e
WordpressExample.rar
(50.22 KB, 下載次數: 7)
- Z+ Q( k, q) H
- Z! l, P8 }; ?, z
* m' H* d- Y5 b
) O0 j9 N: Y/ U2 w7 i: w3 g m% j* ]; f4 h+ A( n9 z
; r) b: R- j2 u8 m0 i
. X+ \8 } k9 { |