这是另一个解决方案
<?php
//Configruations
$endpoints = array(
\'/\' => array( \'homepage\', \'404 Not found\' ),
\'cats\' => array( \'Hobbes\', \'Simba\', \'Grumpy Cat\' ),
\'dogs\' => array( \'Hobbes Dog\', \'Simba Dog\', \'Grumpy Dog\' ),
\'birds\' => array( \'Hobbes Bird\', \'Simba Bird\', \'Grumpy Bird\' ),
);
//Parsing Request :: Take the last element from endpoint /abc/def/xyz
$endpoint = isset( $_SERVER[\'REQUEST_URI\'] ) ? strtolower( $_SERVER[\'REQUEST_URI\'] ) : "/";
$endpoint = parse_url( $endpoint, PHP_URL_PATH );
$endpoint = array_filter(explode("/", $endpoint));
$endpoint = array_pop($endpoint);
$endpoint = array_key_exists($endpoint, $endpoints) ? $endpoint : "/";
//Print Response
header(\'Content-Type: application/json\');
echo json_encode($endpoints[$endpoint], JSON_PRETTY_PRINT);