Can't find this question so if it's already been asked, I'd really appreciate being redirected!
So I'm creating a RESTful API with PHP but I can't seem to get the parameters from the API call. The API call is ~/database/table?key=value
, where the tilda represents the connection to my server. Below is my code:
$method = $_SERVER['REQUEST_METHOD'];
$request = explode('/', $_SERVER['PATH_INFO']);
$body = json_decode(file_get_contents('php://input'), true);
So here's what I understand about the above code. I put the method into $method
, which would be GET, PUT, etc. $request
then creates an array with each thing separated, which to my understanding would look like :
[database, table?key=value]
, and $body
would take whatever data I attach to the request, which in my case would be JSON.
The problem that I'm running into is that the second value of the array is only getting table
and isn't getting anything after the '?' in the API call. Did I miss something? I thought that explode
only separated by the character that I defined which was '/'.
Thanks!!
The PATH_INFO
didn't contain the query string, it was instead stored in QUERY_STRING
. Sorry for the relatively trivial question and thanks to user3783243!