PHP code example of p3k / micropub

1. Go to this page and download the library: Download p3k/micropub library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

p3k / micropub example snippets


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

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

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

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

$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
}


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;
}


print_r($request->toMf2());