本帖最後由 hudba 於 2013-6-17 02:45 編輯
5 Z/ h2 A& w T; Y3 ~: v b
8 C% F* j3 L; K: A以前用到程序中要調用wordpress api的地方,探索了一下,這裡整理一下發給大家,但願對有需要的朋友有幫助。
0 T* U3 P) e4 E. e! W! A4 V, n) F9 Q2 F2 V1 W L
準備工作:5 w; u0 l: a1 |* h1 d+ b) m- y2 B
使用C#調用,推薦vs2010,這裡有下載:
g, E! z/ D, x8 W3 Ehttp://www.microsoft.com/en-us/download/details.aspx?id=12187 3 `: q! p: R. w) \+ Q
程序和wordpress通信需要使用xmlrpc,需要從這裡下載獲得,(或者從我附帶的壓縮包裡也可以獲得):9 D8 y5 L, f3 e. c) }( H
http://xml-rpc.net/download.html
- M% G9 e4 m- ?8 [6 Iwordpress api的文檔:
( L3 D. }; m' X2 H$ b2 @http://codex.wordpress.org/XML-RPC_WordPress_API
" V% |0 }& g1 ^/ y5 G4 f/ \要點講述:
1 }+ G& F2 _8 y$ L0 P( Ovs裡面建立solution:WordpressExample,然後引用xmlrpc的dll,如下圖:
6 x% Z9 _4 H1 K1 t
! ?+ F" X) K5 n! T% U
/ K5 {0 C0 ]0 q# v9 P如何新建Post?/ p+ r" v% X7 ~
查看wordpress的文檔,找到newPost操作需要傳入的參數:4 A, k! n8 ]/ f8 ^. U
http://codex.wordpress.org/XML-RPC_WordPress_API/Posts#wp.newPost / V8 M N w- _8 [ `% F
' S* W2 T9 |2 I L4 X7 r& g7 z# X4 Z其中,blog_id, username, password是每次調用api都需要傳入的身份信息。如果不是mu版本,blog_id使用0。struct content部分是針對每個操作的具體的參數。不過不是每個參數都必須要傳入。& @: n. T% c9 l6 ~$ ^
3 N M; l! M9 K5 R8 z: {定義api調用接口:
+ I% ?2 N: ]% X: t/ V/ H @調用我們使用的xmlrpc類庫,需要建立一個從IXmlRpcProxy繼承來的interface,我們這裡取名叫IWordpress,然後把調用的每個api操作的method寫出來,因為此類庫使用了反射,所以參數的名字必須要和文檔裡面一樣。api的名字使用屬性標識,例如:[XmlRpcMethod("wp.newPost")],這樣你interface裡面的方法名稱可以取一個可讀性高一點的名字。- public interface IWordpress : IXmlRpcProxy
, ]( s- g$ _& C. v3 N* \. E" a - {
' A6 k% w q5 H3 X - [XmlRpcMethod("wp.newPost")]1 M3 K& r. d0 t8 l" F
- string NewPost(int blog_id, string username, string password, NewPostInput newPostInput);, M7 [) n% W8 M- L
- }
複製代碼 建立一個方法方便程序裡面調用:- public static string NewPost(string url, string username, string password, string title, string content)
* E/ F9 C3 G9 M - {7 t, ?2 {' ?! J/ h
- IWordpress proxy = (IWordpress)XmlRpcProxyGen.Create(typeof(IWordpress));
( j# m- e2 z& h) l - proxy.Url = url;
$ z# @, g1 m2 y+ q - NewPostInput postInput;. _, y* Q. K- f! N/ r+ ]
- postInput.post_title = title;
) t3 g: p$ k" |: { ? - postInput.post_content = content;
% Y5 a. p9 C% }8 [0 m: _4 K6 r9 `3 w - postInput.post_status = "publish";6 @. D& \" m0 M, }! ?
- + }/ _4 d1 P X! `9 W
- string postId = proxy.NewPost(0, username, password, postInput);" \+ f; ?; g8 h$ c5 J
- return postId;6 m u. L. P; o
- }
複製代碼 這裡我們首先通過XmlRpcProxyGen.Create創建了一個剛才IWordpress接口的對象,然後指定wordpress url和傳入參數。/ g) s. O C2 U% L8 `6 |2 l6 x7 t
其中,作為Post內容的參數是個復合類型,所以我建立了一個struct來表示:- public struct NewPostInput
% O" H& c" S/ u( K+ T/ _ - {
/ c7 W) C9 Q5 m. } - public string post_title;( p! q' A7 S1 u: Z( L$ N
- public string post_content;4 n" D7 _, |1 }' y
- public string post_status;% I% O. j6 V/ U
- }
複製代碼 文檔裡面的很多參數是可選的,所以作為例子,這裡只有3個主要參數。注意,參數的名字要和文檔裡面的一樣。
. c( I$ E4 q( v$ X8 x; T2 g0 e; w F% D, l- G
如何獲取Post列表?; f" v9 G4 a2 {
同NewPost一樣,先查看文檔,然後把api的定義寫到IWordpress裡面:- public interface IWordpress : IXmlRpcProxy3 W7 a( p9 H$ Y% S g1 h& B
- {
F, h P- `; I# p - [XmlRpcMethod("wp.newPost")]
5 [& j/ o6 B- _' A& W3 r - string NewPost(int blog_id, string username, string password, NewPostInput newPostInput);" \, `& F7 U. e J7 R
- 7 L5 f; A) _5 `3 K7 J8 q; a; E1 G0 \& A
- [XmlRpcMethod("wp.getPosts")]3 n" k& S" o: L
- XmlRpcStruct[] GetPosts(int blog_id, string username, string password);' V3 U. H- k0 Z5 M
- }
複製代碼 接下來,建立一個方便用戶調用的方法,GetPosts:- public static XmlRpcStruct[] GetPosts(string url, string username, string password)
+ \- [3 v" U9 s* `6 s - {
i z/ G( S% q; k' ]; c - IWordpress wordpress = (IWordpress)XmlRpcProxyGen.Create(typeof(IWordpress));* o X: T& R/ j: B, M1 F8 c. x& G$ f8 x
- wordpress.Url = url;
0 p. N+ h- d2 |/ J - XmlRpcStruct[] ret = wordpress.GetPosts(0, username, password);
, K# Z9 Z7 p! X8 y - return ret;& Y3 d6 L2 j/ t4 A' {
- }
複製代碼 這裡返回的是XmlRpcStruct數組。XmlRpcStruct是可以通過字符串索引內容的,比如:ret[0]["post_title"],可以查詢的字段名稱在文檔的return values裡面有說明:' r& i- } | A9 @, _4 [) o3 C- G
http://codex.wordpress.org/XML-RPC_WordPress_API/Posts#wp.getPost
' S; V9 ]* m1 [7 v! h
9 n) f# C- p: O2 X, f7 H, z$ _% w5 Y6 a$ z
調用wordpress api的url是什麼?
5 c% x3 ^0 N9 }- @8 b) ~wordpress的安裝目錄下面有個xmlrpc.php,調用的url就是這個文件的web地址,比如:
; I( L# J0 U5 C2 t, s e, M% `http://www.example.com/xmlrpc.php
( @, u5 C) \2 J. G s% Y& Y5 _4 Z+ n( q5 ^: J) R0 }" f' s
很少寫東西,一動手才感覺簡單的東西,自己雖然知道,但是表達出來挺彆扭。(由己知彼,對寫出一大堆教程的moon light更加佩服了 )。
/ `0 K ?4 j& q; j0 a7 Q希望能對大家有幫助,為有這方面需要的朋友節約一點時間,附上源代碼供大家動手試試:
% e3 k% Y, @$ Z" z# k! v
WordpressExample.rar
(50.22 KB, 下載次數: 7)
5 ?4 d7 _ ~+ ^5 l6 W1 W5 x
2 u1 G9 }+ \$ w8 V: J2 O- @& X, B
% s( E5 Z5 ~8 I6 `
[7 ^8 C) ^7 I( s- Y1 j' m9 z
- ?! `( A4 \ P' H% O I- q* }! w- O7 W' i0 E j* s$ O
$ c- x( p5 x- ^8 `. D* c
|