有没有使用Metaweblog api的PHP类或资源?我想将此api添加到我自己的cms (如wp)中,以便其他应用程序可以轻松地发布(或...)把它扔出去
发布于 2012-05-04 00:21:38
Implementation of the MetaWeblog API http://www.xmlrpc.com/metaWeblogApi in PHP.
我从我链接的这个脚本中寻找灵感来开发我目前正在使用的实现。请随意使用下面的示例代码作为实现metaweblog API的示例-但请考虑使用现代的XMLRPC库。我已经包含了一个链接,指向示例代码所需的原始"xmlrpc.php“文件的修改版本。
下面是示例代码使用的xmlrpc库:XMLRPC library modified to work with PHP 5.4 - originally written by Keith Devens。
在packagist上进行快速包搜索还提供了许多很好的选项,这些选项在PHP标准方面更具前瞻性。您可以在项目中使用的ZendFramework2 even includes a component具有最小的依赖性(10个包-而不是整个框架)。我强烈建议使用这个示例代码,并且任何新的开发都要使用现代的XMLRPC库。
在这里添加示例代码,以防第一个链接失效:
<?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);
}https://stackoverflow.com/questions/6958031
复制相似问题