PHP code example of haruncpi / wp-api

1. Go to this page and download the library: Download haruncpi/wp-api 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/ */

    

haruncpi / wp-api example snippets


ApiConfig::set_route_file( __DIR__ . '/api-routes.php' )
		->set_namespace( 'MyPlugin\Api' )
		->init();

ApiRoute::get( $prefix, $endpoint, $callback, $auth = false );
ApiRoute::post( $prefix, $endpoint, $callback, $auth = false );

// Multiple route in a prefix group.
ApiRoute::prefix( $prefix, function( ApiRoute $route ) {
    $route->get( $endpoint, $callback, $auth = false );
    $route->post( $endpoint, $callback, $auth = false );
});

ApiRoute::get( 'myplugin/v1', '/me', 'ApiController@me' );

ApiRoute::get( 'myplugin/v1', '/me', 'ApiController@me', 'AuthController@check' );

ApiRoute::get( 'myplugin/v1', '/me', 'ApiController@me' );

ApiRoute::get( 'myplugin/v1', '/me', array( ApiController:class, 'me' ) );

ApiRoute::get( 'myplugin/v1', '/me', array( 'MyPlugin\Api\ApiController', 'me' ) );

ApiRoute::get( 'myplugin/v1', '/me', function() {
    // Do something.
});

ApiRoute::prefix( 'myplugin/v1', function( ApiRoute $route ) {
    $route->get( '/products', 'ApiController@products' );
    $route->get( '/categories', 'ApiController@categories' );
});

// With auth check
ApiRoute::prefix( 'myplugin/v1', function( ApiRoute $route ) {
    $route->get( '/me', 'ApiController@me' );
    $route->get( '/settings', 'ApiController@settings' );
    $route->post( '/logout', 'ApiController@logout' );
})->auth( 'AuthController@check' );