Hi Paul,
I ended up using the WP inbuilt REST API, I only set it up to use the POST method and built around that (terrible practice I know but it works – I did find the response time is disgustingly slow compared to a custom built stand alone PHP API). I used this tutorial to get me on the right track: https://awhitepixel.com/blog/in-depth-guide-in-creating-and-fetching-custom-wp-rest-api-endpoints/
Anyway, I’ll assume you have a custom child theme you’re working from.
In your child theme:
1. Create functions.php if you haven’t already.
2. Add the following action and function to functions.php:
add_action('rest_api_init', function() {
register_rest_route('yourownpath/v1', 'post', array( //register the route to call the WP API from
'methods' => WP_REST_SERVER::CREATABLE, //Set the method (i.e. get, post etc.) WP uses a different naming convention though
'callback' => 'get_awesome_params',
'args' => array(), //can be set to accept specific things other than an undefined array, ref awhitepixels api example, the empty array will accept any key passed to it so if you dont set specific args you will need to perform some sort of validation in the function below.
'permission_callback' => function () {
return true;
}
));
});
function get_awesome_params(WP_REST_Request $request) {
//this is where you can add your own code - i.e. what happens with the keys passed in the POST request, the below just returns a response saying what the keys were to demonstrate the return.
return new WP_REST_Response($parameters, 200);
}
3. Test it:
To consume the API you use the following address structure (in Postman for example):
https://yourwebsite.com/wp-json/yourownpath/v1/post
If you’re passing keys in the string (really turning it into a GET) it would be something like this:
https://yourwebsite.comt/wp-json/yourownpath/v1/post/key1=thing&key2=otherthing
Goodluck!