WordPress API

Ruby has a standard library called xmlrpc which takes care of all xmlrpc stuff, you can even create an XML-RPC server using it. Let’s to get some real word example

Looking for really known application that support XML-RPC then of course WordPress was the first attendee.

So what do we want to do?

  • Say hello to WordPress
  • List all available methods
  • List all available users
  • List all available post
  • Create a new post!
  • Retrieve our created post
  • List all comments on our created post
  1. require 'xmlrpc/client'
  2. opts =
  3. {
  4. host: '172.17.0.2',
  5. path: '/xmlrpc.php',
  6. port: 80,
  7. proxy_host: nil,
  8. proxy_port: nil,
  9. user: 'admin',
  10. password: '123123',
  11. use_ssl: false,
  12. timeout: 30
  13. }
  14. # Create a new instance
  15. server = XMLRPC::Client.new(
  16. opts[:host], opts[:path], opts[:port],
  17. opts[:proxy_host], opts[:proxy_port],
  18. opts[:user], opts[:password],
  19. opts[:use_ssl], opts[:timeout]
  20. )
  21. # Create a new instance takes a hash
  22. server = XMLRPC::Client.new3(opts)
  23. # Say hello to WordPress
  24. response = server.call("demo.sayHello")
  25. # List all available methods
  26. server.call('system.listMethods', 0)
  27. # List all available users
  28. server.call('wp.getAuthors', 0, opts[:user], opts[:password])
  29. # List all available post
  30. response = server.call('wp.getPosts', 0, opts[:user], opts[:password])
  31. # Create a new post!
  32. post =
  33. {
  34. "post_title" => 'Rubyfu vs WP XML-RPC',
  35. "post_name" => 'Rubyfu vs WordPress XML-RPC',
  36. "post_content" => 'This is Pragmatic Rubyfu Post. Thanks for reading',
  37. "post_author" => 2,
  38. "post_status" => 'publish',
  39. "comment_status" => 'open'
  40. }
  41. response = server.call("wp.newPost", 0, opts[:user], opts[:password], post)
  42. # Retrieve created post
  43. response = server.call('wp.getPosts', 0, opts[:user], opts[:password], {"post_type" => "post", "post_status" => "published", "number" => "2", "offset" => "2"})
  44. # List all comments on a specific post
  45. response = server.call('wp.getComments', 0, opts[:user], opts[:password], {"post_id" => 4})

Results

  1. >> # Say hello to WordPress
  2. >> response = server.call("demo.sayHello")
  3. => "Hello!"
  4. >>
  5. >> # List all available methods
  6. >> server.call('system.listMethods', 0)
  7. => ["system.multicall",
  8. "system.listMethods",
  9. "system.getCapabilities",
  10. "demo.addTwoNumbers",
  11. "demo.sayHello",
  12. "pingback.extensions.getPingbacks",
  13. "pingback.ping",
  14. "mt.publishPost",
  15. "mt.getTrackbackPings",
  16. "mt.supportedTextFilters",
  17. ...skipping...
  18. "metaWeblog.newMediaObject",
  19. "metaWeblog.getCategories",
  20. "metaWeblog.getRecentPosts",
  21. "metaWeblog.getPost",
  22. "metaWeblog.editPost",
  23. "metaWeblog.newPost",
  24. ...skipping...
  25. "blogger.deletePost",
  26. "blogger.editPost",
  27. "blogger.newPost",
  28. "blogger.getRecentPosts",
  29. "blogger.getPost",
  30. "blogger.getUserInfo",
  31. "blogger.getUsersBlogs",
  32. "wp.restoreRevision",
  33. "wp.getRevisions",
  34. "wp.getPostTypes",
  35. "wp.getPostType",
  36. ...skipping...
  37. "wp.getPost",
  38. "wp.deletePost",
  39. "wp.editPost",
  40. "wp.newPost",
  41. "wp.getUsersBlogs"]
  42. >>
  43. >> # List all available users
  44. >> server.call('wp.getAuthors', 0, opts[:user], opts[:password])
  45. => [{"user_id"=>"1", "user_login"=>"admin", "display_name"=>"admin"}, {"user_id"=>"3", "user_login"=>"galaxy", "display_name"=>"Galaxy"}, {"user_id"=>"2", "user_login"=>"Rubyfu", "display_name"=>"Rubyfu"}]
  46. >>
  47. >> # List all available post
  48. >> response = server.call('wp.getPosts', 0, opts[:user], opts[:password])
  49. => [{"post_id"=>"4",
  50. "post_title"=>"Rubyfu vs WP XMLRPC",
  51. "post_date"=>#<XMLRPC::DateTime:0x0000000227f3b0 @day=1, @hour=19, @min=44, @month=11, @sec=31, @year=2015>,
  52. "post_date_gmt"=>#<XMLRPC::DateTime:0x0000000227d178 @day=1, @hour=19, @min=44, @month=11, @sec=31, @year=2015>,
  53. "post_modified"=>#<XMLRPC::DateTime:0x000000021d6ee0 @day=1, @hour=19, @min=52, @month=11, @sec=25, @year=2015>,
  54. "post_modified_gmt"=>#<XMLRPC::DateTime:0x000000021d4ca8 @day=1, @hour=19, @min=52, @month=11, @sec=25, @year=2015>,
  55. "post_status"=>"publish",
  56. "post_type"=>"post",
  57. "post_name"=>"rubyfu-vs-wordpress-xmlrpc",
  58. "post_author"=>"2",
  59. "post_password"=>"",
  60. "post_excerpt"=>"",
  61. "post_content"=>"This is Pragmatic Rubyfu Post. Thanks for reading",
  62. "post_parent"=>"0",
  63. "post_mime_type"=>"",
  64. "link"=>"http://172.17.0.2/2015/11/01/rubyfu-vs-wordpress-xmlrpc/",
  65. "guid"=>"http://172.17.0.2/?p=4",
  66. "menu_order"=>0,
  67. "comment_status"=>"open",
  68. "ping_status"=>"open",
  69. "sticky"=>false,
  70. "post_thumbnail"=>[],
  71. "post_format"=>"standard",
  72. "terms"=>
  73. [{"term_id"=>"1", "name"=>"Uncategorized", "slug"=>"uncategorized", "term_group"=>"0", "term_taxonomy_id"=>"1", "taxonomy"=>"category", "description"=>"", "parent"=>"0", "count"=>2, "filter"=>"raw"}],
  74. "custom_fields"=>[]},
  75. {"post_id"=>"1",
  76. "post_title"=>"Hello world!",
  77. "post_date"=>#<XMLRPC::DateTime:0x00000002735580 @day=1, @hour=17, @min=54, @month=11, @sec=14, @year=2015>,
  78. "post_date_gmt"=>#<XMLRPC::DateTime:0x0000000226b130 @day=1, @hour=17, @min=54, @month=11, @sec=14, @year=2015>,
  79. "post_modified"=>#<XMLRPC::DateTime:0x00000002268de0 @day=1, @hour=17, @min=54, @month=11, @sec=14, @year=2015>,
  80. "post_modified_gmt"=>#<XMLRPC::DateTime:0x000000021aea58 @day=1, @hour=17, @min=54, @month=11, @sec=14, @year=2015>,
  81. "post_status"=>"publish",
  82. "post_type"=>"post",
  83. "post_name"=>"hello-world",
  84. "post_author"=>"1",
  85. "post_password"=>"",
  86. "post_excerpt"=>"",
  87. "post_content"=>"Welcome to WordPress. This is your first post. Edit or delete it, then start writing!",
  88. "post_parent"=>"0",
  89. "post_mime_type"=>"",
  90. "link"=>"http://172.17.0.2/2015/11/01/hello-world/",
  91. "guid"=>"http://172.17.0.2/?p=1",
  92. "menu_order"=>0,
  93. "comment_status"=>"open",
  94. "ping_status"=>"open",
  95. "sticky"=>false,
  96. "post_thumbnail"=>[],
  97. "post_format"=>"standard",
  98. "terms"=>
  99. [{"term_id"=>"1", "name"=>"Uncategorized", "slug"=>"uncategorized", "term_group"=>"0", "term_taxonomy_id"=>"1", "taxonomy"=>"category", "description"=>"", "parent"=>"0", "count"=>2, "filter"=>"raw"}],
  100. "custom_fields"=>[]}]
  101. >>
  102. >> # Create a new post!
  103. >> post =
  104. | {
  105. | "post_title" => 'Rubyfu vs WP XML-RPC',
  106. | "post_name" => 'Rubyfu vs WordPress XML-RPC',
  107. | "post_content" => 'This is Pragmatic Rubyfu Post. Thanks for reading',
  108. | "post_author" => 2,
  109. | "post_status" => 'publish',
  110. | "comment_status" => 'open'
  111. | }
  112. => {"post_title"=>"Rubyfu vs WP XML-RPC",
  113. "post_name"=>"Rubyfu vs WordPress XML-RPC",
  114. "post_content"=>"This is Pragmatic Rubyfu Post. Thanks for reading",
  115. "post_author"=>2,
  116. "post_status"=>"publish",
  117. "comment_status"=>"open"}
  118. >> response = server.call("wp.newPost", 0, opts[:user], opts[:password], post)
  119. => "7"
  120. >> # Retrieve created post
  121. >> response = server.call('wp.getPosts', 0, opts[:user], opts[:password], {"post_type" => "post", "post_status" => "published", "number" => "2", "offset" => "2"})
  122. => [{"post_id"=>"3",
  123. "post_title"=>"Auto Draft",
  124. "post_date"=>#<XMLRPC::DateTime:0x0000000225bcd0 @day=1, @hour=19, @min=22, @month=11, @sec=29, @year=2015>,
  125. "post_date_gmt"=>#<XMLRPC::DateTime:0x00000002259a98 @day=1, @hour=19, @min=22, @month=11, @sec=29, @year=2015>,
  126. "post_modified"=>#<XMLRPC::DateTime:0x0000000256b808 @day=1, @hour=19, @min=22, @month=11, @sec=29, @year=2015>,
  127. "post_modified_gmt"=>#<XMLRPC::DateTime:0x000000025695d0 @day=1, @hour=19, @min=22, @month=11, @sec=29, @year=2015>,
  128. "post_status"=>"auto-draft",
  129. "post_type"=>"post",
  130. "post_name"=>"",
  131. "post_author"=>"1",
  132. "post_password"=>"",
  133. "post_excerpt"=>"",
  134. "post_content"=>"",
  135. "post_parent"=>"0",
  136. "post_mime_type"=>"",
  137. "link"=>"http://172.17.0.2/?p=3",
  138. "guid"=>"http://172.17.0.2/?p=3",
  139. "menu_order"=>0,
  140. "comment_status"=>"open",
  141. "ping_status"=>"open",
  142. "sticky"=>false,
  143. "post_thumbnail"=>[],
  144. "post_format"=>"standard",
  145. "terms"=>[],
  146. "custom_fields"=>[]},
  147. {"post_id"=>"1",
  148. "post_title"=>"Hello world!",
  149. "post_date"=>#<XMLRPC::DateTime:0x00000002617298 @day=1, @hour=17, @min=54, @month=11, @sec=14, @year=2015>,
  150. "post_date_gmt"=>#<XMLRPC::DateTime:0x00000002615038 @day=1, @hour=17, @min=54, @month=11, @sec=14, @year=2015>,
  151. "post_modified"=>#<XMLRPC::DateTime:0x000000025e6d28 @day=1, @hour=17, @min=54, @month=11, @sec=14, @year=2015>,
  152. "post_modified_gmt"=>#<XMLRPC::DateTime:0x000000025e4aa0 @day=1, @hour=17, @min=54, @month=11, @sec=14, @year=2015>,
  153. "post_status"=>"publish",
  154. "post_type"=>"post",
  155. "post_name"=>"hello-world",
  156. "post_author"=>"1",
  157. "post_password"=>"",
  158. "post_excerpt"=>"",
  159. "post_content"=>"Welcome to WordPress. This is your first post. Edit or delete it, then start writing!",
  160. "post_parent"=>"0",
  161. "post_mime_type"=>"",
  162. "link"=>"http://172.17.0.2/2015/11/01/hello-world/",
  163. "guid"=>"http://172.17.0.2/?p=1",
  164. "menu_order"=>0,
  165. "comment_status"=>"open",
  166. "ping_status"=>"open",
  167. "sticky"=>false,
  168. "post_thumbnail"=>[],
  169. "post_format"=>"standard",
  170. "terms"=>
  171. [{"term_id"=>"1", "name"=>"Uncategorized", "slug"=>"uncategorized", "term_group"=>"0", "term_taxonomy_id"=>"1", "taxonomy"=>"category", "description"=>"", "parent"=>"0", "count"=>3, "filter"=>"raw"}],
  172. "custom_fields"=>[]}]
  173. ...skipping...
  174. "post_format"=>"standard",
  175. "terms"=>[],
  176. "custom_fields"=>[]},
  177. {"post_id"=>"1",
  178. "post_title"=>"Hello world!",
  179. "post_date"=>#<XMLRPC::DateTime:0x00000002617298 @day=1, @hour=17, @min=54, @month=11, @sec=14, @year=2015>,
  180. "post_date_gmt"=>#<XMLRPC::DateTime:0x00000002615038 @day=1, @hour=17, @min=54, @month=11, @sec=14, @year=2015>,
  181. "post_modified"=>#<XMLRPC::DateTime:0x000000025e6d28 @day=1, @hour=17, @min=54, @month=11, @sec=14, @year=2015>,
  182. "post_modified_gmt"=>#<XMLRPC::DateTime:0x000000025e4aa0 @day=1, @hour=17, @min=54, @month=11, @sec=14, @year=2015>,
  183. "post_status"=>"publish",
  184. "post_type"=>"post",
  185. "post_name"=>"hello-world",
  186. "post_author"=>"1",
  187. "post_password"=>"",
  188. "post_excerpt"=>"",
  189. "post_content"=>"Welcome to WordPress. This is your first post. Edit or delete it, then start writing!",
  190. "post_parent"=>"0",
  191. "post_mime_type"=>"",
  192. "link"=>"http://172.17.0.2/2015/11/01/hello-world/",
  193. "guid"=>"http://172.17.0.2/?p=1",
  194. "menu_order"=>0,
  195. "comment_status"=>"open",
  196. "ping_status"=>"open",
  197. "sticky"=>false,
  198. "post_thumbnail"=>[],
  199. "post_format"=>"standard",
  200. "terms"=>
  201. [{"term_id"=>"1", "name"=>"Uncategorized", "slug"=>"uncategorized", "term_group"=>"0", "term_taxonomy_id"=>"1", "taxonomy"=>"category", "description"=>"", "parent"=>"0", "count"=>3, "filter"=>"raw"}],
  202. "custom_fields"=>[]}]

and here is the new post
WordPress API - 图1

Source: HOW TO PROGRAMATICALLY CONTROL WORDPRESS WITH RUBY USING XML-RPC

More about WordPress XML-RPC