1

Is there any PHP class or resource for using Metaweblog api ? I want to add this api to my own cms (like wp) so that other application could easily post (or ...) throw it

skaffman
  • 398,947
  • 96
  • 818
  • 769
Shahrokhian
  • 1,100
  • 13
  • 28
  • 1
    I've just started looking into this myself. There isn't much (so it isn't just you), but this is where I'll be starting: http://ryanhemelaar.blogsome.com/2009/01/29/using-metaweblog-with-php-xmlrpc/ -It's just a look at the file he uses, it hasn't been explained, but it is commented. – americruiser Feb 18 '12 at 05:54

1 Answers1

3

Implementation of the MetaWeblog API http://www.xmlrpc.com/metaWeblogApi in PHP.

I looked to this script I linked for inspiration to develop the implementation I'm currently using. Feel free to use the example code below as an example of implementing the metaweblog API - but please consider using a modern XMLRPC library. I've included a link to a modified version of the original "xmlrpc.php" file that the example code requires.

Here's the xmlrpc library the example code utilizes: XMLRPC library modified to work with PHP 5.4 - originally written by Keith Devens.

Doing a quick package search on packagist also provides many great options that are much more forward thinking in terms of PHP standards. ZendFramework2 even includes a component you can use in your project with minimal dependencies (10 packages - not the entire framework). I would strongly recommend that this example code, be used as such, and any new development be done with a modern XMLRPC library.

Adding the example code here in case the first link dies:

<?php
/**
 * Skeleton file for MetaWeblog API http://www.xmlrpc.com/metaWeblogApi in PHP
 * Requires Keith Devens' XML-RPC Library http://keithdevens.com/software/xmlrpc and store it as xmlrpc.php in the same folder
 * Written by Daniel Lorch, based heavily on Keith Deven's examples on the Blogger API.
 */

require_once dirname(__FILE__) . '/xmlrpc.php';

function metaWeblog_newPost($params) {
  list($blogid, $username, $password, $struct, $publish) = $params;
  $title = $struct['title'];
  $description = $struct['description'];


  // YOUR CODE:
  $post_id = 0; // id of the post you just created


  XMLRPC_response(XMLRPC_prepare((string)$post_id), WEBLOG_XMLRPC_USERAGENT);
}

function metaWeblog_editPost($params) {
  list($postid, $username, $password, $struct, $publish) = $params;


  // YOUR CODE:
  $result = false; // whether or not the action succeeded


  XMLRPC_response(XMLRPC_prepare((boolean)$result), WEBLOG_XMLRPC_USERAGENT);
}

function metaWeblog_getPost($params) {
  list($postid, $username, $password) = $params;
  $post = array();


  // YOUR CODE:
  $post['userId'] = '1';
  $post['dateCreated'] = XMLRPC_convert_timestamp_to_iso8601(time());
  $post['title'] = 'Replace me';
  $post['content'] = 'Replace me, too';
  $post['postid'] = '1';


  XMLRPC_response(XMLRPC_prepare($post), WEBLOG_XMLRPC_USERAGENT);
}

function XMLRPC_method_not_found($methodName) {
  XMLRPC_error("2", "The method you requested, '$methodName', was not found.", WEBLOG_XMLRPC_USERAGENT);
}

$xmlrpc_methods = array(
    'metaWeblog.newPost'  => 'metaWeblog_newPost',
    'metaWeblog.editPost' => 'metaWeblog_editPost',
    'metaWeblog.getPost'  => 'metaWeblog_getPost'
    );

$xmlrpc_request = XMLRPC_parse($HTTP_RAW_POST_DATA);
$methodName = XMLRPC_getMethodName($xmlrpc_request);
$params = XMLRPC_getParams($xmlrpc_request);

if(!isset($xmlrpc_methods[$methodName])) {
  XMLRPC_method_not_found($methodName);
} else {
  $xmlrpc_methods[$methodName]($params);
}
AndrewPK
  • 6,100
  • 3
  • 32
  • 36
  • Anyone have a copy of Keith Deven's library? That site is down now. – emd Apr 05 '14 at 16:49
  • @emd there's [this](https://github.com/graemian/keithdevens-xmlrpc) but at this point if I have some time I'm just going to re-write this against http://gggeek.github.io/phpxmlrpc/ – AndrewPK Apr 07 '14 at 16:37
  • Thanks @andrewpk, Ii found this: http://scripts.incutio.com/xmlrpc/ which was super easy to use so I used that. – emd Apr 08 '14 at 17:33
  • It may not have been updated but it works just fine on Mavericks! – emd Jun 23 '14 at 22:57
  • @emd it may work just fine in Mavericks, but that doesn't mean it's good practice to use something like that. I've updated my answer with better examples - there are plenty of better libraries available in Packagist now than when this question was first asked. I would strongly suggest any new development be done against a modern forward-thinking implementation. – AndrewPK Jun 24 '14 at 15:26
  • OK, I haven't finished so I can take a look before going further and integrate if it is easy. Thanks for the links. – emd Jun 24 '14 at 19:37