本帖最後由 hudba 於 2013-6-17 02:45 編輯
; r" x! ~, x$ k
- `7 ^( @ a/ I7 {% \以前用到程序中要調用wordpress api的地方,探索了一下,這裡整理一下發給大家,但願對有需要的朋友有幫助。
& `) Q5 g: j5 O2 _. z2 {
# |# w; _/ O4 W& x' P準備工作:$ ~ h4 j; ]/ h! g6 o; X
使用C#調用,推薦vs2010,這裡有下載:6 h8 k6 J. z9 t: Y# d5 A
http://www.microsoft.com/en-us/download/details.aspx?id=12187
' n; ^( L a K1 m4 c程序和wordpress通信需要使用xmlrpc,需要從這裡下載獲得,(或者從我附帶的壓縮包裡也可以獲得):: e" L: [! B- @; ~8 {% i# M
http://xml-rpc.net/download.html 4 s" \$ [# a" W# S+ c; W1 X, Y
wordpress api的文檔:
: g$ ^( z, N- `* Q8 k1 bhttp://codex.wordpress.org/XML-RPC_WordPress_API
& \( l! @/ f P8 d; m要點講述:+ E+ I/ A0 j! O3 \) k7 h
vs裡面建立solution:WordpressExample,然後引用xmlrpc的dll,如下圖:' u1 ?& P/ Q6 [$ _1 G4 S
+ `9 x9 |. A: S& P, p6 t3 ?1 T' x/ {8 t+ @% l7 ], ]
如何新建Post?8 Y, B8 Y6 g% h' F& W O K
查看wordpress的文檔,找到newPost操作需要傳入的參數:
, ?5 X( w c, o6 r4 ?http://codex.wordpress.org/XML-RPC_WordPress_API/Posts#wp.newPost
* D; W! }' H$ E/ y, R
' r& J8 \9 ?; [; l8 y其中,blog_id, username, password是每次調用api都需要傳入的身份信息。如果不是mu版本,blog_id使用0。struct content部分是針對每個操作的具體的參數。不過不是每個參數都必須要傳入。
& U! v# ~+ r4 v6 ]- [" L' i( |2 ?; F# h+ ]$ x9 ?% I9 q$ ~
定義api調用接口:1 f) ^* _2 y, F7 U
調用我們使用的xmlrpc類庫,需要建立一個從IXmlRpcProxy繼承來的interface,我們這裡取名叫IWordpress,然後把調用的每個api操作的method寫出來,因為此類庫使用了反射,所以參數的名字必須要和文檔裡面一樣。api的名字使用屬性標識,例如:[XmlRpcMethod("wp.newPost")],這樣你interface裡面的方法名稱可以取一個可讀性高一點的名字。- public interface IWordpress : IXmlRpcProxy1 s* ?$ s/ s/ k+ D |
- {0 y3 K* S' S8 M! r% [' Z5 H, y
- [XmlRpcMethod("wp.newPost")]
4 G0 ^( J- _+ W5 n9 S. H! f - string NewPost(int blog_id, string username, string password, NewPostInput newPostInput);
; U9 F% f: h7 W; ~% L - }
複製代碼 建立一個方法方便程序裡面調用:- public static string NewPost(string url, string username, string password, string title, string content)
9 `9 r4 G1 s+ Q5 D - {
; y, \ ]5 z) Z, T# X0 }% K& W0 z+ n - IWordpress proxy = (IWordpress)XmlRpcProxyGen.Create(typeof(IWordpress));
9 j7 f9 r4 @8 r7 y* j; g! ^ r - proxy.Url = url;
1 n* _, e4 w, i% { - NewPostInput postInput;
. \; P! y) `) L4 V - postInput.post_title = title;
# h9 _" i' a' n& \3 {5 _ - postInput.post_content = content;
/ H R3 k8 d* T, K; ` - postInput.post_status = "publish";
2 |# C4 x3 ?- k5 k
2 g J5 d8 d: a- n- string postId = proxy.NewPost(0, username, password, postInput);
]4 [+ R) [6 ?1 T& M, G5 E* i - return postId;
# I7 ^: {( o" G+ V: q1 V! q' J+ c - }
複製代碼 這裡我們首先通過XmlRpcProxyGen.Create創建了一個剛才IWordpress接口的對象,然後指定wordpress url和傳入參數。
! }* Q& e6 ?5 {# e% g/ }其中,作為Post內容的參數是個復合類型,所以我建立了一個struct來表示:- public struct NewPostInput
8 Z2 t; A/ j3 [& G [ - {
% D* d, b1 M0 ]8 g, T; h% |+ M! f - public string post_title;4 I* g* ?6 T) Z8 I9 |- s9 p
- public string post_content;
7 Z) v1 ?0 e7 P t2 m! O) ]. e+ J - public string post_status;* \$ ^4 W0 o$ @$ a
- }
複製代碼 文檔裡面的很多參數是可選的,所以作為例子,這裡只有3個主要參數。注意,參數的名字要和文檔裡面的一樣。. O) d5 E+ I! z+ [& W K) R( R
" c3 }# b4 j, W7 |如何獲取Post列表?; X: W1 G9 z* C% S
同NewPost一樣,先查看文檔,然後把api的定義寫到IWordpress裡面:- public interface IWordpress : IXmlRpcProxy7 p2 s! ?6 z0 D( A* o& V3 X
- {
% S0 R6 _/ `- { - [XmlRpcMethod("wp.newPost")]5 k; `7 ^1 A$ Y: F* s. }
- string NewPost(int blog_id, string username, string password, NewPostInput newPostInput);: h- s9 h: ?. B; O6 B0 u
- ; x) d; i" J# o
- [XmlRpcMethod("wp.getPosts")]( s% O1 h9 g# m) C9 P8 M
- XmlRpcStruct[] GetPosts(int blog_id, string username, string password);
. v& x4 f$ h: `4 G$ G! q' n3 D" ? - }
複製代碼 接下來,建立一個方便用戶調用的方法,GetPosts:- public static XmlRpcStruct[] GetPosts(string url, string username, string password); C( |' b& u! J! h! O
- {" N2 v% B3 T( ~, Y2 j, ]& r
- IWordpress wordpress = (IWordpress)XmlRpcProxyGen.Create(typeof(IWordpress));
! d8 I1 c, M b - wordpress.Url = url;
7 F' q' i6 C D. f* d% F' `4 z$ b1 D - XmlRpcStruct[] ret = wordpress.GetPosts(0, username, password);
! a t0 X+ s% K7 s6 a t - return ret;7 Y, m9 Q2 A3 Z0 E$ `: f6 d% L
- }
複製代碼 這裡返回的是XmlRpcStruct數組。XmlRpcStruct是可以通過字符串索引內容的,比如:ret[0]["post_title"],可以查詢的字段名稱在文檔的return values裡面有說明:
, G) Z8 y2 h/ X' b6 f: R. v+ xhttp://codex.wordpress.org/XML-RPC_WordPress_API/Posts#wp.getPost & T0 _+ a( p# D' b
4 R1 @5 ]* D9 A* a1 j# F
0 B3 w0 m; j/ f; m5 P# _% m3 S4 r調用wordpress api的url是什麼?
3 \' u3 u+ W; f/ D* f- ^. Bwordpress的安裝目錄下面有個xmlrpc.php,調用的url就是這個文件的web地址,比如:
: `; n. h, C3 V7 x1 ihttp://www.example.com/xmlrpc.php
, r/ C: A* i# ^4 d
* z, g0 }/ g2 K! p& z% l0 K很少寫東西,一動手才感覺簡單的東西,自己雖然知道,但是表達出來挺彆扭。(由己知彼,對寫出一大堆教程的moon light更加佩服了 )。
X+ r& z W6 \1 D0 }( |希望能對大家有幫助,為有這方面需要的朋友節約一點時間,附上源代碼供大家動手試試:
: d6 z" C+ z% t
WordpressExample.rar
(50.22 KB, 下載次數: 7)
N4 L# B+ J6 C0 v/ M }' C$ u8 G
8 Z5 G; a ^) l7 H8 O$ I B
% D# C Q3 R: o) R) W, @5 i0 l! d: o: k4 @- t" y
' y, {$ n, ~& C" J# S
* y# `6 t1 D5 t, N( L' R |