Download the PHP package marspress/rest-api-endpoint without Composer

On this page you can find all versions of the php package marspress/rest-api-endpoint. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package rest-api-endpoint

MarsPress REST API Endpoints

Installation

Require the composer package in your composer.json with marspress/rest-api-endpoint with minimum dev-main OR run composer require marspress/rest-api-endpoint

Resources

There are WordPress constants for the allowed request methods. It is recommended that you use the constants below, concatenating where necessary.

Usage

You will first need to create a Rest Namespace. Then you will add Endpoint objects to it, and add Parameter objects to your Endpoints.

Rest Namespace

new \MarsPress\RestAPI\Rest_Namespace() takes 4 parameters, 2 required and 2 optional.

Available Methods

Endpoint

new \MarsPress\RestAPI\Endpoint() takes 5 parameters, 3 required and 2 optional.

Available Methods

Parameter

new \MarsPress\RestAPI\Parameter() takes 7 parameters, 1 required and 6 optional.

Example

First we make a new Namespace:

Then we can create an Endpoint:

Then we can create a Parameter:

Then we can add Parameters to the newly created Endpoint:

Finally, we can add the Endpoint to the Rest Namespace:

In the above example, the REST API endpoint we created will be at /wp-json/my_api_namespace/v1/my_endpoint/<pid>. The PID parameter will be passed inside the URL, and it must be a numeric value, finally it will be sanitized into a two decimal formatted float. It then returns a new \WP_REST_Response with a message and a status code of 200.

Method / Class Chaining

It is highly recommended that you use method and class chaining when creating your Rest Namespace, Endpoints, and Parameters.

Here is the above example, but using method and class chaining:

Advanced Topics

X-WP-Nonce Header

If you need to use current user sessions in your Endpoint and Parameter callbacks (e.g. current_user_can() or get_current_user_id()), you must send a generated nonce in the header of your request.

This is particularly useful if you only want users to be able to access their own data through your REST endpoint. E.g. I could call /wp-json/my_api_namespace/v1/my_endpoint/18 to get user data for a user with the ID of 18, but if I am not logged in as the user with the ID 18, I would not be able to access the endpoint.

To generate a nonce and make is usable in your JS scripts, you will need to localize your script as such:

It is important to use wp_rest as the key as that is the key WordPress will use to validate the nonce in the REST API.

In a jQuery AJAX example, you would set it as such:

Nothing further is needed, if you generated and localized your nonce and set the X-WP-Nonce header correctly, your REST endpoint callbacks should automatically know the current user session that is trying to access the endpoint.


All versions of rest-api-endpoint with dependencies

PHP Build Version
Package Version
Requires php Version >=7.4
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package marspress/rest-api-endpoint contains the following files

Loading the files please wait ....