← all projects

micropub-adapter

The easiest way to add micropub support to existing or new applications.
repo

taproot/micropub-adapter

Latest Stable Version License Total Downloads

taproot/micropub-adapter is a simple and flexible way to add Micropub support to any PHP web app using PSR-7.

Quick Links

Installation

taproot/micropub-adapter is currently tested against and compatible with PHP 7.3, 7.4, 8.0, 8.1, 8.2, 8.3, 8.4 and 8.5.

Install taproot/micropub-adapter using composer:

composer.phar require taproot/micropub-adapter
composer.phar install (or composer.phar update)

Versioned releases are GPG signed so you can verify that the code hasn’t been tampered with.

gpg --recv-keys 1C00430B19C6B426922FE534BEF8CE58118AD524
cd vendor/taproot/micropub-adapter
git tag -v v0.1.3 # Replace with the version you have installed

Usage

Subclassing MicropubAdapter

micropub-adapter defines an abstract class, Taproot\Micropub\MicropubAdapter, which implements the request handling logic for micropub and micropub media endpoints. It parses incoming micropub requests and dispatches them to callback methods for each action (create, delete, update, etc.). It handles basic validation and error conditions, normalises incoming data for you, and converts return values from callback methods to valid responses.

All you need to do is subclass MicropubAdapter and implement the relevant callback methods for the actions you want to support. Then, in your app, make an instance of your adapter, and call handleRequest() and handleMediaEndpointRequest() within your micropub and media endpoint requests, respectively.

See the example app for an example of how to subclass and use MicropubAdapter.

Callback Methods

Refer to the API documentation for the paramters and possible return values of each callback. Optional callbacks have a default no-op implementation.

Required for any functionality:

Micropub endpoint action callbacks — implement whichever are relevant for your use-case:

Media Endpoint callbacks — implement to enable the media endpoint. As routing is out of the scope of this library, you’ll have to add a media-endpoint value to the array returned by configurationQueryCallback() in order for clients to be able to discover the media endpoint.

Extension callbacks — these are called after the incoming request is authenticated, but before any micropub-specific handling occurs. This allows your subclass to implement micropub extensions. Implementations of these methods should check to see if a request requires extension handling (e.g. a ?q=source request without a url parameter, which would return an error if handled by the logic surrounding sourceQueryCallback()). If the request requires extension handling, handle it and return a Response or error value. Otherwise, return false to continue handling the request as usual.

Access Tokens and IndieAuth

You’ll need some way of verifying the access tokens used to authenticate micropub requests — indeed, verifyAccessTokenCallback() is the only method which you’re absolutely required to implement! If your app doesn’t yet have IndieAuth endpoints capable of creating and verifying access tokens, you may want to use the companion library taproot/indieauth to add indieauth support to your app. The example app uses taproot/indieauth, so you can refer to that for an example of how to use the two libraries together.

Contributing

If you have any questions about using this library, join the indieweb chatroom and ping barnaby.

If you find a bug or problem with the library, or want to suggest a feature, please create an issue.

If discussions lead to you wanting to submit a pull request, following this process, while not required, will increase the chances of it quickly being accepted:

Changelog

Unreleased

v0.1.3

2023-07-24

(0.1.2 missing due to tagging of incorrect commit)

v0.1.1

2022-10-03

v0.1.0

2021-06-24

Initial release.