本帖最後由 hudba 於 2013-6-17 02:45 編輯 ) l. a0 {2 G. G1 F4 E
o0 h! p# b& U, {" H/ X以前用到程序中要調用wordpress api的地方,探索了一下,這裡整理一下發給大家,但願對有需要的朋友有幫助。
3 z8 j, [! P" `* V# M
2 V2 X. W% Z. ~% q準備工作:
* { f Z! X0 }使用C#調用,推薦vs2010,這裡有下載:
) H9 d; E. d$ n& K- whttp://www.microsoft.com/en-us/download/details.aspx?id=12187
{& K% x8 N1 x程序和wordpress通信需要使用xmlrpc,需要從這裡下載獲得,(或者從我附帶的壓縮包裡也可以獲得):9 ^6 I- B. J; ?- `# D: w! d
http://xml-rpc.net/download.html ; H$ K9 @; k. P& u% j
wordpress api的文檔:
K8 H; x7 l- {1 G0 Q, m+ G2 ahttp://codex.wordpress.org/XML-RPC_WordPress_API
! s- f+ \) {4 G: p. L要點講述:% h$ w6 B7 [2 g7 }( ^ q! J3 ?
vs裡面建立solution:WordpressExample,然後引用xmlrpc的dll,如下圖:
4 }( v- y# v: U2 A$ {& h; V
E" }6 w( K1 y4 Q0 j; V$ e }2 H1 J t Y+ ~$ S: I2 N
如何新建Post?) S. n. u' I* x1 P
查看wordpress的文檔,找到newPost操作需要傳入的參數:! S! i; |& ^. @) \5 z% u
http://codex.wordpress.org/XML-RPC_WordPress_API/Posts#wp.newPost
L1 q. X/ K( [4 [2 \0 y! }/ _
, C6 g; H9 A7 {! d其中,blog_id, username, password是每次調用api都需要傳入的身份信息。如果不是mu版本,blog_id使用0。struct content部分是針對每個操作的具體的參數。不過不是每個參數都必須要傳入。
! ~8 T- _9 M8 ~1 N% v
; r; t7 ~' V$ A2 d% x定義api調用接口:
2 Y7 k& ?) J( J* ]' I調用我們使用的xmlrpc類庫,需要建立一個從IXmlRpcProxy繼承來的interface,我們這裡取名叫IWordpress,然後把調用的每個api操作的method寫出來,因為此類庫使用了反射,所以參數的名字必須要和文檔裡面一樣。api的名字使用屬性標識,例如:[XmlRpcMethod("wp.newPost")],這樣你interface裡面的方法名稱可以取一個可讀性高一點的名字。- public interface IWordpress : IXmlRpcProxy
: b2 K# [; x k+ |( \, P* q$ u0 i - {
/ g, i0 c3 H* d; u' ?9 Z - [XmlRpcMethod("wp.newPost")]0 B) k/ o" k7 x/ J0 v8 P
- string NewPost(int blog_id, string username, string password, NewPostInput newPostInput);# a! j K5 n5 L' ]3 w' }) q
- }
複製代碼 建立一個方法方便程序裡面調用:- public static string NewPost(string url, string username, string password, string title, string content)
8 u( ^5 j& e5 _$ \ - {7 o) J4 G; Q. |- S
- IWordpress proxy = (IWordpress)XmlRpcProxyGen.Create(typeof(IWordpress));6 q' j8 P/ w) a
- proxy.Url = url;- |! R4 K! D- f7 y, I
- NewPostInput postInput;: g5 M# I1 H: P; s) H3 T8 r
- postInput.post_title = title;
0 Z# H( S/ ` V ]/ U - postInput.post_content = content;
2 y9 S0 k- T9 R2 ~ j7 U( { - postInput.post_status = "publish";
# ^" `2 Y; s; f- F7 O - " c+ g6 l2 _6 M7 w( a) J. T
- string postId = proxy.NewPost(0, username, password, postInput); {1 d7 q8 |% ]$ ^: c
- return postId;6 e# O) |/ V4 E/ D" d+ m
- }
複製代碼 這裡我們首先通過XmlRpcProxyGen.Create創建了一個剛才IWordpress接口的對象,然後指定wordpress url和傳入參數。
3 G2 c0 {0 Q# b其中,作為Post內容的參數是個復合類型,所以我建立了一個struct來表示:- public struct NewPostInput6 z2 d) M- ]2 @' `( F4 d" Q
- {. j, V3 O& |5 @+ C) \8 D! L8 X0 q1 B! T
- public string post_title;
. j! D4 L; _7 a' i. x# O - public string post_content;
) ?6 l% m. V% K8 S - public string post_status;
2 q0 G( Q" q1 r - }
複製代碼 文檔裡面的很多參數是可選的,所以作為例子,這裡只有3個主要參數。注意,參數的名字要和文檔裡面的一樣。2 j0 ~7 x4 W$ Q. I
5 X8 c2 l5 o g5 \ F+ O如何獲取Post列表?$ n; o C5 d Z
同NewPost一樣,先查看文檔,然後把api的定義寫到IWordpress裡面:- public interface IWordpress : IXmlRpcProxy
& t" t3 ^* i6 C. k* U - {
8 e9 U: Q P' Z' V - [XmlRpcMethod("wp.newPost")]
( ^' N0 {4 r: D4 P5 l - string NewPost(int blog_id, string username, string password, NewPostInput newPostInput);8 Y& A) J! @/ g6 u5 M
* R- N5 h! d; {8 I+ B- [XmlRpcMethod("wp.getPosts")]; z, P% {; \( y9 K) d4 d$ C
- XmlRpcStruct[] GetPosts(int blog_id, string username, string password);" a( m4 R7 R, R! o9 g2 B
- }
複製代碼 接下來,建立一個方便用戶調用的方法,GetPosts:- public static XmlRpcStruct[] GetPosts(string url, string username, string password)# H7 o( I5 |8 [+ p
- {
& X8 c6 V5 T- o7 a% G - IWordpress wordpress = (IWordpress)XmlRpcProxyGen.Create(typeof(IWordpress));0 p4 i4 n& w- H
- wordpress.Url = url;
( W9 B3 {7 n( l d# @ - XmlRpcStruct[] ret = wordpress.GetPosts(0, username, password); u+ ?& \( U3 B' d2 D* E: o# X
- return ret;9 y1 T3 k$ P7 m( ?
- }
複製代碼 這裡返回的是XmlRpcStruct數組。XmlRpcStruct是可以通過字符串索引內容的,比如:ret[0]["post_title"],可以查詢的字段名稱在文檔的return values裡面有說明:+ f; d% Z! q& u( n, q
http://codex.wordpress.org/XML-RPC_WordPress_API/Posts#wp.getPost
+ F0 X6 D. h8 I( G$ ?
/ O8 P6 }9 z& e) m4 l* c+ T
+ \* F6 u7 y% `7 x$ M6 @; ?0 y) P調用wordpress api的url是什麼?
, M, n. [ N6 w: K' F8 ^% ^wordpress的安裝目錄下面有個xmlrpc.php,調用的url就是這個文件的web地址,比如:& Q4 M) k! c! D4 [( A
http://www.example.com/xmlrpc.php
- u7 B3 v" _) ]; C$ d. I
3 l, Q8 \7 a) B) D很少寫東西,一動手才感覺簡單的東西,自己雖然知道,但是表達出來挺彆扭。(由己知彼,對寫出一大堆教程的moon light更加佩服了 )。
7 c6 k4 s8 T) `& ~希望能對大家有幫助,為有這方面需要的朋友節約一點時間,附上源代碼供大家動手試試:
X* n5 H: S; m" \ H3 p
WordpressExample.rar
(50.22 KB, 下載次數: 7)
# _3 V1 Z/ R# g n+ e2 Y9 n1 P* t; C& t6 B. }
- y) \ A8 n, m, V: T) t
( M, {( x/ u S4 H) ^ M2 ^- A9 X+ @- A
: @) S! A2 h4 W8 y/ [0 ]
2 F. E5 [# ?1 ]2 W; e* N, f |