Skip to content
Advertisement

Handling PHP URL and GET requests

I am currently parsing any given url, for example example.com/page/1 or example.com/api/info/1, by exploding the URL:

JavaScript

and then accessing its elements in the array like so:

JavaScript

But how could I handle (and access via array in the same way) a url that also includes GET elements, such as example.com/page?id=1 or example.com/api/info?=hello ?

I am using nginx and trying files like below:

JavaScript

Advertisement

Answer

You can parse URL by parse_url function also for query params you can use parse_str function and extract all query parameters.

JavaScript

Output:

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement