So, for those of you who read my last blog post, you might notice that I was having issues with a script I wrote to create a new post in Wordpress when it came time to upgrade the Wordpress code.  The SQL internals were modified, and I was inserting directly in to the database (yes, I know, I broke a cardinal rule).  I needed an alternative way to insert information in to the database that would be much more future-proof.  I remembered about XML-RPC.

It took me some time to find answers to questions I had about XML-RPC and the Wordpress’ API.  Wordpress comes with the ability to use XML-RPC, and AtomPub.  With regards to XML-RPC, it supports a few protocols:

  1. MetaWeblog
  2. Movable Type
  3. Blogger
  4. Wordpress’ own methods

Since there was little written documentation as to how to do this, I thought I’d share my findings.  Also, although the Blogger API was very easy to figure out and use, I’m not going to cover it here mainly because it doesn’t support the creation of a title field – something for my purposes were required.  (To access the blogger API, I used this class.)  I’m also only going to cover what’s necessary to create a new post for my own means, nothing more – hopefully it’ll get you on your way to how you’d like to use it.


One thing to note: In order for this to work, your version of Wordpress will have to have “Remote Publishing” enabled; depending on which protocol(s) you’ll be using, you’ll want to enable either “Atom Publishing Protocol” or “XML-RPC”, or both. This can be found (as of Wordpress version 2.7) under “Settings” and then “Writing”.

Let’s get to some nitty-gritty. The following XML-RPC methods are allowable with Wordpress (again, version 2.7):

Wordpress API:

  1. wp.getUsersBlogs
  2. wp.getPage
  3. wp.getPages
  4. wp.newPage
  5. wp.deletePage
  6. wp.editPage
  7. wp.getPageList
  8. wp.getAuthors
  9. wp.getCategories
  10. wp.getTags
  11. wp.newCategory
  12. wp.deleteCategory
  13. wp.suggestCategories
  14. wp.uploadFile
  15. wp.getCommentCount
  16. wp.getPostStatusList
  17. wp.getPageStatusList
  18. wp.getPageTemplates
  19. wp.getOptions
  20. wp.setOptions
  21. wp.getComment
  22. wp.getComments
  23. wp.deleteComment
  24. wp.editComment
  25. wp.newComment
  26. wp.getCommentStatusList

Blogger API:

  1. blogger.getUsersBlogs
  2. blogger.getUserInfo
  3. blogger.getPost
  4. blogger.getRecentPost
  5. blogger.getTemplate
  6. blogger.setTemplate
  7. blogger.newPost
  8. blogger.editPost
  9. blogger.deletePost

MetaWeblog API:

  1. metaWeblog.newPost
  2. metaWeblog.editPost
  3. metaWeblog.getPost
  4. metaWeblog.getRecentPosts
  5. metaWeblog.getCategories
  6. metaWeblog.newMediaObject
  7. metaWeblog.deletePost
  8. metaWeblog.getTemplate
  9. metaWeblog.setTemplate
  10. metaWeblog.getUsersBlogs

MovableType API:

  1. mt.getCategoryList
  2. mt.getRecentPostTitles
  3. mt.getPostCategories
  4. mt.setPostCategories
  5. mt.supportedMethods
  6. mt.supportedTextFilters
  7. mt.getTrackbackPings
  8. mt.publishPost

Wordpress also supports pingback, supposedly as an API. As for the AtomPub API, I don’t really understand that one yet, so if you want to find out what’s supported, you’ll want to look through the wp-app.php file found in your main Wordpress folder. The XML-RPC methods can be found in the xmlrpc.php file, also found in the main Wordpress folder.

As you can see, the Wordpress API is jam-packed with features – I believe most desktop applications that allow you to moderate your comments use the Wordpress API. Unfortunately, no “New Post” option for our needs. Dang. Because of this, and also because the Blogger API doesn’t support the creation of a title field in a new post, I decided to go with the MetaWeblog API. Technically speaking, MovableType and AtomPub are superior options to both Blogger and MetaWeblog from what I’ve been reading; regardless of that (possible) fact, I found it easy to work with the MetaWeblog API.

Let’s get to the code…

Umm…can PHP handle the transformation from data to the XML-RPC protocol of our choice automatically?

Unfortunately, no. However, the creators of Wordpress’ API features thought ahead. They packaged Wordpress with certain features and functionality (and classes) that can be utilized by us mere mortals to achieve our goals. Many of these libraries (such as the one we’ll use) were written by those not affiliated with Wordpress – so they took the legwork of finding good, well-written libraries for us.

We’ll be using the Incutio XML-RPC Library for PHP created by Simon Willison. You could download this yourself and include it in your script (note: commenter believes the official download to be corrupted), or you could just use what Wordpress already has in its folder structure, it’s the same thing with a few (pertinent) modifications and a different file name. (Wordpress uses it internally for some features since it works as a client and a server.)

The file is class-IXR.php, found in your wp-includes folder. So, to start out, we’ll create a PHP file that includes that script. We’ll also need to instantiate a new client object (to talk to the XML-RPC server – aka Wordpress). To find out what the address of your Wordpress XML-RPC server is (in order to instantiate the client), after you’ve enabled the XML-RPC remote publishing in your Wordpress settings, go to your Wordpress blog (any non-admin page) and view the source.

<link rel=”EditURI” type=”application/rsd+xml” title=”RSD” href=”http://www.example.com/xmlrpc.php?rsd />

You’ll want to take the URL from the HREF property of that line of code and place it in your browser. The rendered XML of the page gives us information as to the target URI for the protocol(s) we’d be using. If you’ll be using multiple protocols that use multiple targets, you’d have to target them each individually when making client calls. So, on to the fun stuff…

This includes our XML-RPC library, allowing us to create a client connection. It then uses (defines) the target server access point for our client to access the APIs we aim to use. xmlrpc.php supports Wordpress, MovableType, MetaWeblog, and Blogger APIs all in one access point which makes it very easy if you want to use any or all four of them with the same IXR_Client object instantiation. Let’s build on that some more.

This extra code now tells our client to query our server for the Wordpress API’s “getCategories” method using the username of “admin” with a password of “password”. If any errors occur, the script will stop execution after printing the error codes and messages supplied by Wordpress’ API server. If it’s successful, the server’s response is stored in the $response variable.

Taking a step back, the getCategories method takes on 3 parameters. I left one blank simply because Wordpress (as of version 2.7) does not require the BlogID field (WPMU might, however). The 3 parameters are BlogID, Username, and Password. It will authenticate the user before allowing information to be returned.

The Wordpress API is quite easy (or at least for the getCategories method anyway), it simply returns a PHP structured array. An example print_r output from the $response variable would look like the following:

That’s pretty darn easy to break down to values we might need! Now, moving on, let’s add some code to create a new post.

This code sets up our desired blog title, the category we wish to use (in string format, do not use the ID value), the body of the blog post, and then queries the XML-RPC server. It calls the MetaWeblog API’s “New Post” method, and passes it some values. The parameters for this method are as follows:

Parameter Description
BlogID not used (yet)
Username the authenticated username that has permission to create posts via XML-RPC
Password the username’s associated password
Content the ARRAY of values that will be converted to XML by our XML-RPC library, and sent to Wordpress. There are a plethora of array key values that Wordpress supports for this data structure, but many of them are Wordpress specific. If you wish to remain more true to the specification, the ones I’ve shown are about all you’ve got.
Publish a boolean value to tell whether or not to immediately publish the post; in this example, it will create a draft entry, requiring someone to check it before publishing.

So we query the Wordpress XML-RPC server to create our new blog post, and if it’s successful, it returns the newly created blog posts’ ID field. The neat thing about this, is that (again, as of Wordpress version 2.7), if you wish to preview a blog post, you can use your Wordpress installation’s URL and adding a short query string to the end with this blog ID. For example, let’s assume this returned the ID of 3, the preview URL would then be:

http://www.example.com/?p=3

Pretty easy, huh? Here’s the full sample code without any breaks:

Hopefully having this documented somewhere will help someone out. I’ve used it at the work to allow librarians to upload exported XML New Item Record reports to the server, let it be processed, and then post the processed output to a Wordpress Draft where it can be reviewed for any irregularities or errors, and then posted for our public to see. The end result of that can be found here: http://www.sspl.org/newmaterials/


Trackbacks


Trackback specific URI for this entry
    Edit a Wordpress Article with PHP and XML-RPC
    I had created a topic discussing how to create an interface to add new posts to a Wordpress blog using PHP and XML-RPC. It was something I had needed for work. Unfortunately for many of my site visitors and readers from Google, I never had the need to edi
    Weblog: mysiteonline™
    Tracked: May 09, 21:17

Comments


    #1 Mike on 02/12/09 at 08:07 AM [Reply]
    I couldn't really understand why you were going to all this trouble until the last paragraph. But the new materials page looks good and you don't have to intervene so that's good. Me, I'm still doing the new materials lists myself but it's okay...it's not very time consuming! ;)
    #1.1 Harpreet Singh on 03/04/10 at 04:37 AM [Reply]
    Hello

    I have subscribed from news agency and they can deliver news directly
    to my server in xml.

    I want to publish these articles from the xml file to wordpress blog.

    Please help
    #1.1.1 Brendon Kozlowski on 03/04/10 at 06:33 AM [Reply]
    Hello Harpreet,

    I have provided all of the code examples necessary to automatically post an article to a Wordpress blog via PHP. PHP also contains many useful classes and functions when dealing with XML data (SimpleXML, DOMDocument (DOM XML), XPath, expat, etc... Choosing the proper tool for your data would depend on the business requirements you have, and the structure of the XML. As there are many ways to manage this, I cannot directly help you with that, you would need to do the necessary research to decide what solution is best for your specific case.

    It should not take too long if you have any PHP background. I was using this solution (automatically posting to Wordpress) for an XML data file as well. Once the data is parsed, it's quite straight forward. Parsing the data should be easy enough once you choose your parsing solution within PHP (or other language).
    #1.1.1.1 Brendon Kozlowski on 03/04/10 at 06:42 AM [Reply]
    For anyone wanting to do more with XML-RPC, I recently found a class written by someone else that does a whole heck of a lot. It's great to understand what's going on and why (my simple tutorial), but if you do need more, this is a nice option (assuming it still works): http://blog.5ubliminal.com/posts/remote-control-wordpress-blog-xmlrpc-api/
    #2 Brendon Kozlowski on 02/12/09 at 10:54 AM [Reply]
    Well, I tagged the article as "PHP" rather than "Work", so that's why the last paragraph is the last paragraph and not the first. ;) Regardless, it really was a pain trying to find any examples of how to use these tools together, even though I knew the ability was there, so I wanted to document my process. It truly does open up a whole range of possibilities for those that work with Wordpress (or similar XML-RPC compliant tools).

    No, it's not very time consuming at all, which is handy. If I had direct access to our catalog's database I'd probably completely automate the process and integrate it to our website rather than use Wordpress, but alas... :P At least we can get XML exported data files!
    #3 Dave W. on 02/12/09 at 12:13 PM [Reply]
    Thanks so much for sharing this, it is exactly what I needed.
    #4 Teft on 03/16/09 at 11:16 PM [Reply]
    Seriously excellent info! I searched and tested for 3 hours with junk results!! Then, found your post, and within 5 minutes had my code working with wordpress. Awesome. Thanks again. Teft.
    #5 free puke on 03/19/09 at 04:38 AM [Reply]
    Thanks man - there aren't many informations about that out there and you helped me a lot :-) btw. the download link for the class on the creators website seems to be corrupted lol :-( good that you can steal it from any WP installation ^^.
    #5.1 Tom on 04/04/13 at 10:29 PM [Reply]
    Not sure if this is still being monitored... but here goes.. this comment applies to many of the errors above... I had huge pains getting my version to work and I received the same errors no matter what i did... Turns out that the 32700 error in my case was caused by the quotes around the password... I notice that the 'user' was used around the user name however a different charracter was used around the password.. changing the character from what ever it is to 'password' made it work like a charm.... Thank you so much Brendon, you are a life saver... its works beautifully....
    Thank you! Thank you! and Thank you! and oh by the way did I remember to say Thank you!
    #5.1.1 Brendon Kozlowski on 04/12/13 at 09:11 AM [Reply]
    I do still receive email notices when people leave comments, so in that regard it's still monitored. I've been looking to switch blogging engines for a long time now and just kind of gave up on the site in the meantime.

    As for your errors, I'll try to leave a note at the top of the article to mention that - thank you for bringing it up. I can't seem to force the blogging engine to keep my standard quotes when in code blocks. Sorry about that - but glad that once you figured out the problem it worked out for you! Thank you for your feedback!
    #6 Brendon Kozlowski on 03/19/09 at 06:54 AM [Reply]
    Interesting website you have there! I'm glad this helped you out; also thanks for letting me know that the official Incutio library's download might be bad, I've edited my post to reference that. Good luck on the rest of your endeavors!
    #7 Me on 04/02/09 at 10:44 AM [Reply]
    Hi --

    Interesting thanks -- but do you have *any* idea how to modify the actual wordpress 'date'?

    I am trying
    $content['dateCreated']="2010-10-10 10:10:10";
    as an example

    But when I do this I get an error saying "-32700 error in xml format"...

    I can figure out how to do this and am becoming exceptionally frustrated :( (Have already spent three hours on a stupid little thing and the wordpress documentation sucks)... :P sorry, just a bit annoyed... If you could help that would be awesome!
    #8 Me on 04/02/09 at 10:45 AM [Reply]
    pps, I have also tried gmdate(...) that doesn't work either.
    #9 kisso on 04/03/09 at 05:31 AM [Reply]
    I want to know how to use wp.getUsersBlogs with java me (the code for it)
    I'm using WTK emulator.

    if anyone could help
    thx:)
    #10 Brendon Kozlowski on 04/03/09 at 07:50 AM [Reply]
    1. I'm sort of busy and although I do get notifications whenever a comment is made, I may not have time to respond.

    2. I will NOT (personally) help with something outside the realm of what was discussed in this article (i.e.: Java / WTK emulator).

    3. "Me" - in case you're able to find an answer before me, I wanted to add a few bits of knowledge that might help. When I was researching this stuff, I *think* someone mentioned that datetime values had to be in a specific format for it to work. If it wasn't, the expected result would not be achieved. Also, I can't recall for certain, but the MetaWeblog API might not be able to specify a date different than that of when the API was called. I believe AtomPub supports different dates, but I haven't tried learning that API.

    4. For any information that you can't seem to find answers for on the vast WWW, you may want to try the wp-xmlrpc mailing list: http://comox.textdrive.com/pipermail/wp-xmlrpc/
    #11 Hi Brendon on 05/12/09 at 02:04 PM [Reply]
    I copied your code and got this error...

    32300:transport error - could not open socket: 0 php_network_getaddresses: getaddrinfo failed: Name or service not known

    Any ideas?
    #12 Brendon Kozlowski on 05/14/09 at 09:08 AM [Reply]
    @Hi Brendon: (lol, I had to)

    If all you did is copy my code and try to use it (without reading the whole explanation and process steps), the only thing that comes to mind off hand would be that you did not enable your "Remote Publishing" in Wordpress. It's the 4th paragraph down (skipping the list, I didn't count that as a paragraph).

    FYI - for anyone else trying to leave a comment. My comments are auto-moderated after 30 days from the initial post. Sorry for no mention of this in my theme.
    #13 Casual on 07/01/09 at 12:09 AM [Reply]
    Thank you! This is exactly what I needed! I've spent hours of frustration trying to find a solution that works, and this is the first one that has worked flawlessly! Looking forward to tweaking the code to fit my needs.
    #14 Brendon Kozlowski on 07/01/09 at 08:55 AM [Reply]
    I'm very glad to hear that this helped you out. Thank you for your feedback.
    #15 Prabhu on 07/31/09 at 08:52 AM [Reply]
    I cant Get how can i post comment to blogger using this code please give me some explaination
    #15.1 Brendon Kozlowski on 07/31/09 at 12:16 PM [Reply]
    This article is about Wordpress, not Blogger. Although the XMLRPC library used here should theoretically work with Blogger, I don't believe the code examples I've given here will. They were designed to operate with Wordpress only.
    #16 Casual on 07/31/09 at 01:26 PM [Reply]
    FYI, this is a site I made using the code. It's a directory site that people can submit their sites to, and the form automatically creates a post for me-- I just need to approve or reject it:

    http://www.seductionlist.com/submit/

    Thanks again!
    #16.1 Brendon Kozlowski on 07/31/09 at 05:32 PM [Reply]
    I'm glad it's helping others out - thank you for commenting to let me know!
    #17 GoTop on 10/08/09 at 11:14 PM [Reply]
    when i want to publish the post, i use this script,
    $client->query('metaWeblog.newPost','', 'admin',’password’, $content, true))

    but it return an error,

    An error occurred - -32301:transport error - HTTP status code was not 200

    how to figure out this problem ?
    #17.1 Brendon Kozlowski on 10/09/09 at 07:59 AM [Reply]
    Are you certain that you've enabled your XMLRPC functionality within Wordpress? If so, did you manually test it by browsing to the URL suggested in my article to be sure that there are no caching issues from other plugins? If so...

    ...a 200 HTML response means that the page was successfully found and rendered. If the error message you're seeing is "Status code was not 200", that tells me that there may be an error in the URL you are using to access your Wordpress' XMLRPC mechanism. Unfortunately I no longer have a Wordpress installation that I can test this with to see if an incorrectly given URL to the XMLRPC call will generate that error message.

    If you're using the Incutio library included with Wordpress, there is a way to debug the output to check your data. After creating your new IXR_Client object, set it's debug property to 1.

    $client = new IXR_Client('http://xmlrpc.example.com');
    $client->debug = 1;
    #17.1.1 GoTop on 10/09/09 at 10:32 AM [Reply]
    Hi,thanks for reply so quickly.

    i'm sure i had enabled my XMLRPC functionality within Wordpress.

    and i use this to post a post as a draft,it work find for me.
    $client->query('metaWeblog.newPost','', 'admin',’password’, $content, false)


    but when i want to post a publish post with this script,
    $client->query('metaWeblog.editPost', $fm_post_id, USER, PASSWORD, $content, true)

    i get an error,
    An error occurred - -32301:transport error - HTTP status code was not 200

    i set debug property to 1 as you told me, but the error code is too long i can't post it in comment.
    #17.1.1.1 Brendon Kozlowski on 10/29/09 at 10:34 AM [Reply]
    First of all, I'd like to apologize for not approving your comment and also for not replying sooner. I did get notified of your comment via email, but completely forgot to take care of it, and time crept by. My sincerest apologies.

    As for the code...Ah, now I see the difference. You're using the editPost method, not the newPost. To be honest, I never looked at any other functions in-depth except for the single one that I needed for this project, and that was newPost. As I no longer have a test/development Wordpress install to toy with (the project is still live, but it's production, I can't mess around with it), I don't think I'll be able to be the best source of help on this issue. I would suggest checking the Wordpress community forum, some web searches, the API itself, and possibly even the mailing lists if necessary.

    I'm sorry I can't be more helpful. :(
    #17.1.1.1.1 GoTop on 11/08/09 at 01:16 PM [Reply]
    Dude, never mind.

    i finally find another way to work it out.

    i use xmlrpc-3.0.0.beta library, and everything work fine except it's a little bit slower than using IXR library.

    cheer
    #17.1.1.1.1.1 Brendon Kozlowski on 11/09/09 at 12:06 PM [Reply]
    I'm glad you were able to find an answer.
    #18 Haider on 12/07/09 at 09:11 PM [Reply]
    Thank you very much for this explanation and code. It really helped me to fetch new posts in each blog from outside WPMU 2.8.5.2 (i needed this to integrate WPMU and our main website.)

    I used metaWeblog.getRecentPosts for each blog. Now the problem is that each blog must have XML-RPC enabled by default but that's not the case as for each blog this option is disabled by default. So is there a way to enable XML-RPC in Settings->Writings->Remote Publishing by default for each new blog that is created? Or is there any better alternative to using XML-RPC to achieve the integration purpose in our scenario?

    Thanks Again!
    #18.1 Brendon Kozlowski on 12/08/09 at 07:20 PM [Reply]
    I see you asked a similar question over at the WPMU support site. Did the response help out, or were you looking specifically to enable all site's Remote Publishing instead of the simple solution provided by the plugin in the reply at:
    http://mu.wordpress.org/forums/topic/15814
    ?
    #19 Morten Skogly on 12/11/09 at 04:35 PM [Reply]
    Superb! It took me hours to find your tutorial, and I've been struggling with several awful examples. Thank you!

    I did a test trying to display the content from a page and it works great! Here is my code for getting the description from a page with id 4682 on my blog.


    if (!$client->query('wp.getPage','','4682', 'username','password')) {
    die('An error occurred - '.$client->getErrorCode().":".$client->getErrorMessage());
    }
    $pagetest = $client->getResponse();


    echo $pagetest["description"];

    Go to http://codex.wordpress.org/XML-RPC_wp to find more examples on what you can pull out of wordpress using this method
    #20 Ale.Sasha on 01/09/10 at 03:55 AM [Reply]
    Great! Thank you! This is really what I was searching for!!!

    Which are fields allowed in "$content" var? Is there a doc about it=
    #20.1 Brendon Kozlowski on 01/11/10 at 02:20 PM [Reply]
    Hello. When I did the research to create this article and my code, I believe I did find a document that listed available fields that Wordpress supported in addition to the standard fields that are supported in the standard MetaWeblog API. Unfortunately, I was unable to determine where it was, or what they were.

    The following discussion topic might shed some light on the issue though:
    http://wordpress.org/support/topic/328404
    #21 zlatan on 02/13/10 at 10:07 AM [Reply]
    It's been a years and 2 days since you wrote this post... and your good will is still echoing through the universe.

    Thanks. It was very helpful.

    May you have all the money, pussy, power, and anything else you want in this life :)

    Take care.
    #21.1 Brendon Kozlowski on 02/17/10 at 07:20 AM [Reply]
    Umm...thanks, I think? Ha! I'm glad that it's still helping people. I've been tempted to close comments on this post since I no longer work with Wordpress, but considering how helpful it has been to some people, I thought that might be a little unfair. Again, I'm glad that it has been helpful to you!
    #22 suranjith on 03/14/10 at 06:07 PM [Reply]
    Thanks very useful indeed.
    #23 Katie on 03/15/10 at 05:01 AM [Reply]
    Your code worked pretty well but what about fetching all the posts already there in the system and creating a panel where we can edit any of the selected post and publish back to the system. is it possible?
    #23.1 Brendon Kozlowski on 03/15/10 at 04:21 PM [Reply]
    Hi Katie,

    Although this is most definitely possible, it would require using more than just the MetaWeblog methods - probably a lot of the Wordpress-specific methods. However, modifying records within Wordpress, especially if it's customized or using a WYSIWYG, may not be the most optimal way to manage this. It might be better to use a Wordpress plugin that creates it more of a CMS than it is, with user access controls and permissions. I don't really work with Wordpress, so I unfortunately can't really recommend a best-of-breed approach. But yes, it is possible.
    #24 Katie on 04/15/10 at 06:26 AM [Reply]
    It would be cool if you give example for metaWeblog.editPost I am almost there but something is wrong i suppose. Waiting for your example now.
    #24.1 Brendon Kozlowski on 05/09/10 at 09:18 PM [Reply]
    I now have an example of how to use metaWeblog.editPost.

    http://life.mysiteonline.org/archives/198-Edit-a-Wordpress-Article-with-PHP-and-XML-RPC.html
    #25 Uttam on 07/07/10 at 04:26 AM [Reply]
    I am using wordpress 2.9.1 and PHP 4.2.3.
    I have activated XML-RPC service through wordpress admin.

    I have installed Qumana web client to post content from local machine.
    I entered end point as "www.xyz.com/xmlrpc.php" with username and password.
    But after trying to connect my blog it shows error as below:
    "Error trying to retrieve your blog information. Error decoding XML-RPC response: The processing instruction target matching "[xX][mM][][] is not allowed."
    Please provide me proper solution.
    #26 Lee Graham on 07/16/10 at 07:09 AM [Reply]
    Hi Brendan,
    This is briliant, like many posting comments here, I have been searching for information on how to do this but until now I didi not find the information or it was way too complicated for me to put together with my limited knowledge of php.

    Now at last I have a solution, thanks to you.

    thank you
    Lee
    url]http://socialmediacraze.com/[/url]
    #26.1 Brendon Kozlowski on 07/16/10 at 07:24 PM [Reply]
    I'm very glad that you found it useful!
    #27 Joe Alex on 08/16/10 at 08:02 AM [Reply]
    This information is very helpful. unfortunately i'am a techie that understand php. is there any plugins that can help me maka autoposting from bulk article in a random time?
    #27.1 Brendon Kozlowski on 08/17/10 at 11:10 AM [Reply]
    Joe, unfortunately I am not clear on what you are asking. Do you understand PHP, or do you not understand PHP?

    As far as a plugin to help you autopost from bulk articles, not only do I know the situation in which you are looking for an answer, but you'd probably be better off asking this type of question at the Wordpress website community discussion area: http://wordpress.org/support/
    #28 Argan wijaya on 08/19/10 at 12:24 PM [Reply]
    hey i'm not sure how to use this tutorial, where i must put this code to?
    am i must make new file or where?
    #28.1 Brendon Kozlowski on 08/19/10 at 05:21 PM [Reply]
    Hello,

    The script can be placed on any PHP enabled server, so long as it is given a PHP file extension (so the server knows to run it as PHP). The URL must point to your Wordpress installation's proper path, as explained in the article. The username and password fields must be changed to match your Wordpress username and password. The include path must point to a version of the Wordpress class-IXR.php file, so if not on the same server/account as the Wordpress installation, then you may need to download a copy of that file to include it.

    Everything else should be explained within this article.
    #29 Friendly Froggy on 09/09/10 at 10:50 AM [Reply]
    Hi !

    First I would like to thank you for this piece of code. I've spent the whole day trying to post on WP using XMLRPC without any result.

    I can get connected and return categories for instance, but I can't publish anything with your script. I always get :

    An error occurred - -32700:parse error. not well formed

    I think metaWeblog.newPost is not working anymore... Could you confirm this ?
    #29.1 Brendon Kozlowski on 09/15/10 at 06:49 PM [Reply]
    Actually, I've confirmed that it still is working when I wrote a quick followup article to manage Wordpress XMLRPC calls to edit an article (rather than create one).

    The error message sounds like it's telling you exactly what the problem is: "not well formed". Try creating a new post with a very simple message, such as "Hello World" and a title of "New". Do not include any other fields other than the title and body.

    There is also a debug option (which I mention in a comment in this post, search the page for "debug" and you'll find some example code).

    There are some answers to questions found within the comments on this page. :)
    #29.1.1 Friendly Froggy on 09/16/10 at 06:33 AM [Reply]
    Thanks for your answer

    I found the solution puting content in a array.

    Now it works fine.

    $content = array(
    'title'=>$title,
    'description'=>$article,
    'mt_allow_comments'=>$allowcomments,
    'mt_allow_pings'=>$allowtrackbacks,
    'post_type'=>'post',
    'mt_keywords'=>$keywords,
    'categories'=>array($category)
    #30 Brendon Kozlowski on 09/23/10 at 07:18 PM [Reply]
    Hi. I'm glad to see that you figured it out!
    #31 Ste on 10/01/10 at 05:16 AM [Reply]
    Good list man, should come in handy when i need to post to my blog remotely.

    Ste
    #32 Y8 on 10/16/10 at 01:00 AM [Reply]
    I have a category id. So how to post in this category id?
    #33 Y8 on 10/17/10 at 09:33 PM [Reply]
    How to post in categories 12,15 by method below?

    $method_name = “metaWeblog.newPost”;
    $rpc_url = $rpcfile;
    $blog_id = 1;
    $publish = true;
    $post = array(
    “title” => $title,
    “wp_slug” => createurl($title),
    “description” => $contents,
    “categories” => $cat,
    “mt_keywords” => $tags,
    “custom_fields” => $custom_fields
    );

    $rpc = new IXR_Client( $rpc_url );
    $status = $rpc->query(
    $method_name,
    $blog_id,
    $username,
    $password,
    $post,
    $publish
    );

    if( !$status ) {
    print “Error in RPC request\n”;
    print_r( $rpc );
    exit;
    }

    if( $rpc->getResponse( ) ){
    $sql=”INSERT INTO `grab_table` (`url`) values (‘$url’)”;
    if (mysql_query($sql)){
    echo “Add!”;

    }
    }
    #33.1 Brendon Kozlowski on 10/27/10 at 11:25 AM [Reply]
    The example code above shows how to get all categories. Even if your website has 100 categories (WAY too many), PHP should be easily (and quickly) able to retrieve a list of categories. If you only know the numbers, loop through the results to get the necessary data that's needed to post to those particular categories using the code above as a guideline.
    #34 OldManRiver on 10/26/10 at 07:16 AM [Reply]
    All,

    Like what I am reading here, but have what I think is a unique Q about this process.

    Q:

    I have a directory on my local machine (laptop) Ex: "c:\somedir" where I have several .html content files. I want to add these as subposts to a main post (showing as tab, parentid=0 in MySQL DB). I know to do this I must be able to get the "ID" of the parent post, before the post will register under the parent and I found out, using ScribeFire that XML-RPC natively writes to home directory or parent level, then requiring manual intervention to move it as sub-post under the parent post.

    My ISP, for security reasons, only allows 'localhost' transactions so I can not us host domain name or IP to gain access to the MySQL DB, so running my PHP script, which on the local machine see my directory fine, cannot see the local directory, when I post my script to the host.

    Therefore that is why I am looking at the XML-RPC method as I should be able to post this way, leaving the script on my local machine.

    The only problem I see is how to set up my posts so they correctly post under the parent, without requiring manual "MOVE" intervention.

    I would appreciate knowing the correct extra code needed here to get this done.

    Thanks!

    OMR
    #34.1 Brendon Kozlowski on 10/27/10 at 11:36 AM [Reply]
    Unfortunately I am not well-versed in Wordpress development, or the extensiveness of its XML-RPC functions. I only found this information out due to a need in a project. Because of that, I'm not entirely sure I completely understand your situation, but more unfortunate is that I am entirely sure that I wouldn't know how to help.

    I'd suggest asking for help at the Wordpress Support Forums as they should be able to (hopefully) provide much more superior support.
    #35 OldManRiver on 10/26/10 at 07:28 AM [Reply]
    Also your code shows

    CODE:
    include("../wp-includes/class-IXR.php"); $client = new IXR_Client('http://127.0.0.1/wordpress/xmlrpc.php'); if (!$client->query('wp.getCategories','', 'admin','password')) {     die('An error occurred - '.$client->getErrorCode().":".$client->getErrorMessage()); } $response = $client->getResponse();


    What about accessing from local script with:
    CODE:
    include("http://mydomain.com/wp-includes/class-IXR.php"); $client = new IXR_Client('http://mydomain/wordpress/xmlrpc.php'); if (!$client->query('wp.getCategories','', 'admin','password')) {     die('An error occurred - '.$client->getErrorCode().":".$client->getErrorMessage()); } $response = $client->getResponse();


    OMR
    #35.1 Brendon Kozlowski on 10/27/10 at 11:29 AM [Reply]
    That would not work. If you want to work locally you'd need the class-IXR.php file local to the script you're running. If you request a file from a different server that is written in a language which would typically be rendered by the server before being displayed to the visitor (like PHP), you won't get the actual code, just a rendering of what the code should do. Since this is just a class library and no actual code is being executed, you'd effectively be including a blank file from a separate server.
    #36 V_RocKs on 02/17/11 at 05:40 PM [Reply]
    So happy people like you blog your thoughts and post your scripts. I was just about to do all of this work myself and you were #5 in Google on xmlrpc and posting to wordpress from a php script!
    #36.1 Brendon Kozlowski on 02/17/11 at 06:25 PM [Reply]
    I'm very glad that it's helped you out!
    #37 Zack on 03/25/11 at 07:50 AM [Reply]
    Thank you so much for posting this. For whatever reason it was hard for me to find any good information on actually USING the xmlrpc stuff with blogs. This was a great tutorial and I was posting to wordpress in less than 10 minutes with it.
    #37.1 Brendon Kozlowski on 03/31/11 at 12:01 PM [Reply]
    Hey, no problem. I'm very glad it was able to help you out!
    #38 James on 05/06/11 at 09:26 AM [Reply]
    Hello... Nice code. Does the job for my wordpress sites. But I get an error on my other (do not own)WP remote posting sites. I do not have permission to turn on writing.php for xml-rpc.
    The ERROR message below ............

    An error occurred - 405:XML-RPC services are disabled on this blog. An admin user can enable them at http://www.articlebooth.com/wp-admin/options-writing.php

    Any other php options/ideas/ways/methods/phpcode to get around this problem. Other then ubots or imacros????

    Thanks James
    #38.1 Brendon Kozlowski on 06/08/11 at 12:16 PM [Reply]
    Hello James. The only thing I could think of would be using some sort of ugly cURL hack. I'm not too familiar with Wordpress (or cURL) unfortunately - I only found this solution because I needed it for something and decided to share it. It would be easier to just request the site owner to enable that feature.
    #39 Zip Server on 05/21/11 at 06:11 PM [Reply]
    Great tutorial of API usage. Thanks for "unstucking" me.
    #39.1 Brendon Kozlowski on 06/08/11 at 12:17 PM [Reply]
    Thank you. I'm glad you found it useful!
    #40 jamminjames on 07/11/11 at 02:02 PM [Reply]
    Hi,

    I'm not very proficient in PHP, but as far as I can tell from your description, this code could be used to display someone's blogger posts in my Wordpress site, correct?

    I used the example script in a page at http://wp.humortimes.com/&Wordpress-import-blog.php, but I get an error saying "Parse error: syntax error, unexpected T_INCLUDE in /usr/home/wheel/humortimes/wordpress/&Wordpress-import-blog.php on line 2"

    What am I missing?

    Also, is the password info to be included on the
    if (!$client->query('wp.getCategories'
    line different from the info on the
    if (!$client->query('metaWeblog.newPost'
    line? Is one for the Wordpress site, and the other for the blog you're importing?

    Thanks for any help you can offer!
    #40.1 Brendon Kozlowski on 10/14/11 at 07:09 AM [Reply]
    Hi jamminjames, sorry for my really late response. I get so much spam on this blog I often times miss actual comments and loathe having to go through the comment approval process. :P

    The password info should not be any different. It's the user account information you'd be using (one that has permission to create a blog post).

    As for your T_INCLUDE error...it sounds like you have a syntax error somewhere. If you've doctored up your code at all, try simplifying it back down first and work your way back to more complicated code (if you're new to PHP anyway).
    #41 Androideur on 07/14/11 at 03:57 AM [Reply]
    That's exactly what I need !!!
    Thanks
    #42 Invalid XML on 09/07/11 at 04:16 PM [Reply]
    This is really cool, I never understood API feeds that well until you laid it out here.

    Is there a plugin that can do this as well?
    #42.1 Brendon Kozlowski on 10/14/11 at 07:16 AM [Reply]
    There is a plugin, yes. I believe I mention it in a comment somewhere here and an updated edit to the article itself...
    #43 codersguru on 09/10/11 at 10:28 AM [Reply]
    This helped me to make my Evil script, using this code + HTML parse DOM i managed to copy anyone's database. Thank you Brendon
    #43.1 Brendon Kozlowski on 10/14/11 at 07:00 AM [Reply]
    Umm... Well I'm glad it helped you to complete your script, but parsing and copying a DOM doesn't let you copy anyone's database.
    #44 Rajeev on 11/29/11 at 07:14 AM [Reply]
    Thanks for the info. The api calls list is pretty useful.

    I managed to work out a script that uses a data feed to submit blogs to wordpress via xml-rpc. People can find more details here:
    http://www.home-working.info/index.php/creating-a-wordpress-website-via-xml-rpc-using-affiliate-data-feeds/

    Cheers
    Raj
    #45 victor on 12/02/11 at 04:07 PM [Reply]
    Great code,
    however i have check amny browser addon that uses the xmlrpc. but no one that works inside wysiwyg client in wordpress.

    my client want to be avle to
    copy and paste image to wordpress from front end and i have fixed that with nicedit but need it to also upload the image directly when clinet copy from ms word that include iamges to wordpres

    any ideas?
    #46 mua sam vui on 03/01/12 at 02:14 AM [Reply]
    I am in processing to create auto post and thanks for sharing this infomation !
    #47 haxburgen on 03/01/12 at 03:56 PM [Reply]
    This is super helpful. I spent countless hours scouring the entire internet for something like this. Keep it up!
    #48 Jono on 03/19/12 at 06:42 AM [Reply]
    Thanks a lot man!
    You are right, this is not well documented on the net.
    This was a big help!
    #49 Dan Fry on 04/11/12 at 08:47 AM [Reply]
    Thankyou for this post, I just used parts of it to make my own php submitter, it works a treat and allows me to process items and them post the results..

    This was very useful!!
    #49.1 Brendon Kozlowski on 04/11/12 at 09:01 AM [Reply]
    Glad to hear it helped you out, Dan!
    #50 SH Code on 07/27/12 at 12:33 AM [Reply]
    Yay!!! now we can manage post using XML-RPC for Wordpress

    http://codex.wordpress.org/XML-RPC_WordPress_API
    #50.1 Brendon Kozlowski on 08/14/12 at 12:19 PM [Reply]
    That. Is. Awesome. Thanks for the heads up!
    #51 Karl Peterson on 08/13/12 at 04:24 PM [Reply]
    I was getting a load of errors with the code, here is the updated version that works!!

    query('wp.getCategories','', 'WHERE-I-PUT-MY-WORDPRESS-USERNAME','WHERE-I-PUT-MY-WORDPRESS-PASSWORD')) {
    echo('An error occurred calling wp.getCategories - '.$client->getErrorCode().":".$client->getErrorMessage());
    }
    $response = $client->getResponse();




    $content = array(
    'title'=>$title,
    'description'=>$article,
    'mt_allow_comments'=>$allowcomments,
    'mt_allow_pings'=>$allowtrackbacks,
    'post_type'=>'post',
    'mt_keywords'=>$keywords,
    'categories'=>array($categories)
    );

    var_dump($content); // paste the output of this back to the forum for us to review.

    if (!$client->query('metaWeblog.newPost','', 'WHERE-I-PUT-MY-WORDPRESS-USERNAME','WHERE-I-PUT-MY-WORDPRESS-PASSWORD', $content, false)) {
    echo('An error occurred calling metaWeblog.newPost - '.$client->getErrorCode().":".$client->getErrorMessage());
    }
    echo $client->getResponse(); //with Wordpress, will report the ID of the new post
    ?>
    #51.1 Brendon Kozlowski on 08/14/12 at 12:21 PM [Reply]
    Hi Karl. I'm sorry you were getting errors. I'm still using the same code that I've put up here in production, even through many updates and revisions of Wordpress itself. I have not had any issues thus far. I believe my CMS might be stripping some characters from my code examples though. I haven't looked closely to compare, but thank you for posting your version of a working solution!

Add Comment

E-Mail addresses will not be displayed and will only be used for E-Mail notifications.

To prevent automated Bots from commentspamming, please enter the string you see in the image below in the appropriate input box. Your comment will only be submitted if the strings match. Please ensure that your browser supports and accepts cookies, or your comment cannot be verified correctly.
CAPTCHA

BBCode format allowed