p3k/micropub

服务器和客户端构建时处理 Micropub 请求的实用工具

0.0.3 2018-04-16 14:05 UTC

This package is auto-updated.

Last update: 2024-08-29 04:41:58 UTC


README

Build Status

用法

创建请求对象

如果你不知道客户端是否发送了表单编码或 JSON 请求,你可以使用以下方法自动检测输入类型并创建请求对象。你可以将原始字符串输入传递给此函数,或者一个数组。

$input = file_get_contents('php://input');
$request = \p3k\Micropub\Request::create($input);

如果你使用的是像 Laravel 这样处理输入解析的框架,你可以传递一个包含输入的数组

$request = \p3k\Micropub\Request::create(Request::all());

表单编码输入

如果你知道你正在处理表单编码请求,请根据表单编码输入创建新的 Micropub 请求对象

$request = \p3k\Micropub\Request::createFromPostArray($_POST);

JSON 输入

根据 JSON 输入数组创建新的 Micropub 请求对象

$input = json_decode(file_get_contents('php://input'), true);
$request = \p3k\Micropub\Request::createFromJSONObject($input);

(这实际上适用于从 JSON 创建的对象或数组,但内部使用数组,因此首先将其解码为数组更高效。)

处理错误

如果输入数据无法解释为 Micropub 请求,则返回的对象将是错误。你可以通过测试返回对象的类型是否为 \p3k\Micropub\Error 来检查此点,或者你可以测试 error 属性。

$request = \p3k\Micropub\Request::createFromPostArray($_POST);

if($request->error) {
  // Something went wrong. More information is available here:
  // $request->error_property
  // $request->error_description
}

if(get_class($request) == \p3k\Micropub\Error::class) {
  // Another way to test for errors
}

处理请求

现在你已经有了对应于 Micropub 请求的 $request 对象,你可以检查它以确定如何处理请求。以下是一个说明基本 Micropub 工作流程的概要。

switch($request->action) {
  case 'create':

    // The type of Microformats object being created:
    $request->type; // typically this is `h-entry`

    // All the values of the post are available in this array:
    $request->properties;

    /* 
      {
        "content": ["This is a plaintext note"]
      }

      {
        "name": ["Hello World"],
        "content": [{"html": "This is an <i>HTML</i> blog post"]
      }
    */

    // Any Micropub actions such as mp-syndicate-to are available in this array:
    foreach($request->commands as $command=>$value) {
      switch($command) {
        case 'mp-syndicate-to': 
          // ...
          break;
        // ... etc
      }
    }

    break;
  case 'update':
    // The URL being updated is available here
    $request->url;

    // ... retrieve the post given by that URL

    // Update actions have three parts: replace, add and delete
    // See https://www.w3.org/TR/micropub/#update for more details

    foreach($request->update['replace'] as $key=>$value) {

    }

    foreach($request->update['add'] as $key=>$value) {
      
    }

    foreach($request->update['delete'] as $key=>$value) {
      
    }

    break;
  case 'delete':
    // The URL being deleted is available here:
    $request->url;

    // ... you'll want to retrieve that post and delete it

    break;
}

以 Microformats JSON 输出

你可以将 Micropub 请求输出为 Microformats JSON 数组。

print_r($request->toMf2());

许可证

版权所有 2018 年 Aaron Parecki

Apache 2.0 和 MIT 许可下可用。

Apache 2.0

根据 Apache License,版本 2.0(“许可证”);除非根据适用法律或书面同意,否则不得使用此文件,除非符合许可证。您可以在以下位置获得许可证副本:

https://apache.ac.cn/licenses/LICENSE-2.0

除非适用法律要求或书面同意,否则在许可证下分发的软件按“原样”提供,不提供任何明示或暗示的保证,包括但不限于适销性、适用于特定目的和无侵权性保证。有关许可证的具体语言,请参阅许可证,以了解管理许可权限和限制的具体语言。

MIT

特此授予任何获得此软件及其相关文档文件(“软件”)副本的任何人免费权利,以无限制地处理该软件,包括但不限于使用、复制、修改、合并、发布、分发、再许可和/或出售软件副本,并允许向提供软件的个人授予这样做,受以下条件约束

上述版权声明和本许可声明应包含在所有副本或软件的大量部分中。

软件按“原样”提供,不提供任何形式的保证,无论是明示的还是暗示的,包括但不限于适销性、适用于特定目的和无侵权性保证。在任何情况下,作者或版权所有者不对任何索赔、损害或其他责任承担责任,无论是基于合同、侵权或其他原因,无论是否源于、因之或与此软件或其使用或其他交易有关。