PHP MetaWeblog API 服务器

发布于 2024-11-28 00:44:55 字数 91 浏览 1 评论 0原文

有没有使用 Metaweblog api 的 PHP 类或资源? 我想将此 api 添加到我自己的 cms(如 wp)中,以便其他应用程序可以轻松发布(或...)抛出它

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

傲娇萝莉攻 2024-12-05 00:44:55

在 PHP 中实现 MetaWeblog API http://www.xmlrpc.com/metaWeblogApi。

我查看了我链接的这个脚本,以获取开发我当前正在使用的实现的灵感。请随意使用下面的示例代码作为实现元博客 API 的示例 - 但请考虑使用现代 XMLRPC 库。我包含了示例代码所需的原始“xmlrpc.php”文件的修改版本的链接。

以下是示例代码使用的 xmlrpc 库:经过修改以与 PHP 5.4 配合使用的 XMLRPC 库 - 最初由 Keith Devens 编写。

packagist 上进行快速软件包搜索还提供了许多很棒的选项,这些选项在术语上更具前瞻性PHP 标准。 ZendFramework2 甚至包含一个组件,您可以在项目中使用最少的依赖项(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);
}

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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文