Setting relative path to ElementAPI in PHP
You need the url, not the file path. Can you store the url, or the http://localhost:8888 part of it in an environment variable? That gives you
$url = getenv('CRAFT_URL') . $id . &quot;.json&quot;;

If it is not already in an environment variable, then in a .env file? web/index.php that comes with Craft is a good example of loading a .env file, and then using the environment variable.