If you want a more user-friendly URL structure, you could add a rewrite endpoint.
This will add the rules for and intercept requests to /my-api/
:
function wpd_add_api_endpoint() {
add_rewrite_endpoint( 'my-api', EP_ROOT );
}
add_action( 'init', 'wpd_add_api_endpoint' );
function wpd_api_request( $request ){
if( isset( $request->query_vars['my-api'] ) ){
// $request->query_vars['my-api'] is a string containing everything after your url slug
// convert it to array
$parts = explode( "https://wordpress.stackexchange.com/", $request->query_vars['my-api'] );
echo '<pre>';
print_r( $parts );
echo '</pre>';
die;
}
}
add_action( 'parse_request', 'wpd_api_request' );
After you flush rules, try a request like:
Which will output:
Array
(
[0] => something
[1] => else
[2] => 123
[3] => 4,5,6
[4] => &7
)