wordpress的XML-RPC开发

wordpress支持使用xml-rpc进行开发。但网络上很少有相关的资料。这两天在做一个离线写blog的东东,研究到了这个东西,顺便做一下记录。

wordpress的xml-rpc支持多种API,根据从xmlrpc.php里面提取的数据,支持的API有:

// Blogger API
 ‘blogger.getUsersBlogs’ => ‘this:blogger_getUsersBlogs’,
 ‘blogger.getUserInfo’ => ‘this:blogger_getUserInfo’,
 ‘blogger.getPost’ => ‘this:blogger_getPost’,
 ‘blogger.getRecentPosts’ => ‘this:blogger_getRecentPosts’,
 ‘blogger.getTemplate’ => ‘this:blogger_getTemplate’,
 ‘blogger.setTemplate’ => ‘this:blogger_setTemplate’,
 ‘blogger.newPost’ => ‘this:blogger_newPost’,
 ‘blogger.editPost’ => ‘this:blogger_editPost’,
 ‘blogger.deletePost’ => ‘this:blogger_deletePost’,

// MetaWeblog API (with MT extensions to structs)
 ‘metaWeblog.newPost’ => ‘this:mw_newPost’,
 ‘metaWeblog.editPost’ => ‘this:mw_editPost’,
 ‘metaWeblog.getPost’ => ‘this:mw_getPost’,
 ‘metaWeblog.getRecentPosts’ => ‘this:mw_getRecentPosts’,
 ‘metaWeblog.getCategories’ => ‘this:mw_getCategories’,
 ‘metaWeblog.newMediaObject’ => ‘this:mw_newMediaObject’,

// MetaWeblog API aliases for Blogger API
 // see http://www.xmlrpc.com/stories/storyReader$2460
 ‘metaWeblog.deletePost’ => ‘this:blogger_deletePost’,
 ‘metaWeblog.getTemplate’ => ‘this:blogger_getTemplate’,
 ‘metaWeblog.setTemplate’ => ‘this:blogger_setTemplate’,
 ‘metaWeblog.getUsersBlogs’ => ‘this:blogger_getUsersBlogs’,

// MovableType API
 ‘mt.getCategoryList’ => ‘this:mt_getCategoryList’,
 ‘mt.getRecentPostTitles’ => ‘this:mt_getRecentPostTitles’,
 ‘mt.getPostCategories’ => ‘this:mt_getPostCategories’,
 ‘mt.setPostCategories’ => ‘this:mt_setPostCategories’,
 ‘mt.supportedMethods’ => ‘this:mt_supportedMethods’,
 ‘mt.supportedTextFilters’ => ‘this:mt_supportedTextFilters’,
 ‘mt.getTrackbackPings’ => ‘this:mt_getTrackbackPings’,
 ‘mt.publishPost’ => ‘this:mt_publishPost’,

// PingBack
 ‘pingback.ping’ => ‘this:pingback_ping’,
 ‘pingback.extensions.getPingbacks’ => ‘this:pingback_extensions_getPingbacks’,

‘demo.sayHello’ => ‘this:sayHello’,
 ‘demo.addTwoNumbers’ => ‘this:addTwoNumbers’

其中最后两个API为用于测试的DEMO。

根据XML-RPC的约定,客户端必须向服务器作以下post:

POST /RPC2 HTTP/1.0
 User-Agent: Frontier/5.1.2 (WinNT)
 Host: betty.userland.com
 Content-Type: text/xml
 Content-length: 181

<?xml version="1.0"?>
 <methodCall>
 <methodName>examples.getStateName</methodName>
 <params>
 <param>
 <value><i4>41</i4></value>
 </param>
 </params>
 </methodCall>

User-Agent和Host项是必须的。

Content-Type的值必须是text/xml.

Content-Length必须指定,而且必须是正确的值。

下面是一个例子:

POST /api/RPC2 HTTP/1.0
 User-Agent: Java.Net Wa-Wa 2.0
 Host: plant.blogger.com
 Content-Type: text/xml
 Content-length: 515

<?xml version="1.0"?>
 <methodCall>
 <methodName>blogger.newPost</methodName>
 <params>
 <param><value><string>C6CE3FFB3174106584CBB250C0B0519BF4E294</string></value></param>
 <param><value><string>744145</string></value></param>
 <param><value><string>ewilliams</string></value></param>
 <param><value><string>secret</string></value></param>
 <param><value><string>Today I had a peanut butter and pickle
 sandwich
 for lunch. Do you like peanut-butter and pickle sandwiches? I do.
 They’re yummy. Please comment!</string></value></param>
 <param><value><boolean>false</boolean></value></param>
 
 </params>
 </methodCall>

服务器返回的数据:

HTTP/1.1 200 OK
 Connection: close
 Content-Length: 125
 Content-Type: text/xml
 Date: Mon, 6 Aug 20001 19:55:08 GMT
 Server: Java.Net Wa-Wa/Linux

<?xml version="1.0"?>
 <methodResponse>
 <params>
 <param>
 <value><string>4515151</string></value>
 </param>
 </params>
 </methodResponse>

如果出现错误,服务器会返回以下内容:

HTTP/1.1 200 OK
 Connection: close
 Content-Length: 225
 Content-Type: text/xml
 Date: Mon, 6 Aug 20001 19:55:08 GMT
 Server: Java.Net Wa-Wa/Linux

<?xml version="1.0"?>
 <methodResponse>
 <fault>
 <value>
 <struct>
 <member>
 <name>faultCode</name>
 <value><int>4</int></value>
 </member>
 <member>
 <name>faultString</name>
 <value><string>java.lang.Exception: java.lang.Exception: Error:
 AppKey is
 invalid or inactive.</string></value>
 </member>
 </struct>
 </value>
 </fault>
 </methodResponse>