php get all params
Get URL query string parameters
What is the «less code needed» way to get parameters from a URL query string which is formatted like the following?
Output should be: myqueryhash
I am aware of this approach:
11 Answers 11
$_SERVER[‘QUERY_STRING’] contains the data that you are looking for.
DOCUMENTATION
The PHP way to do it is using the function parse_url, which parses a URL and return its components. Including the query string.
The function parse_str() automatically reads all query parameters into an array.
EDIT
If you want the whole query string:
I will recommended best answer as
The above example will output:
This code and notation is not mine. Evan K solves a multi value same name query with a custom function 😉 is taken from:
It bears mentioning that the parse_str builtin does NOT process a query string in the CGI standard way, when it comes to duplicate fields. If multiple fields of the same name exist in a query string, every other web processing language would read them into an array, but PHP silently overwrites them:
Instead, PHP uses a non-standards compliant practice of including brackets in fieldnames to achieve the same effect.
This can be confusing for anyone who’s used to the CGI standard, so keep it in mind. As an alternative, I use a «proper» querystring parser function:
How to get parameters from a URL string?
How can I get only the email parameter from these URLs/values?
Please note that I am not getting these strings from browser address bar.
13 Answers 13
You can use the parse_url() and parse_str() for that.
will extract the emails from urls.
Use the parse_url() and parse_str() methods. parse_url() will parse a URL string into an associative array of its parts. Since you only want a single part of the URL, you can use a shortcut to return a string value with just the part you want. Next, parse_str() will create variables for each of the parameters in the query string. I don’t like polluting the current context, so providing a second parameter puts all the variables into an associative array.
As mentioned in other answer, best solution is using
parse_url()
The parse_url() parse URL and return its components that you can get query string using query key. Then you should use parse_str() that parse query string and return values into variable.
Also you can do this work using regex.
preg_match()
You can use preg_match() to get specific value of query string from URL.
preg_replace()
Also you can use preg_replace() to do this work in one line!
I created function from @Ruel answer. You can use this:
This is working great for me using php
A much more secure answer that I’m surprised is not mentioned here yet:
So in the case of the question you can use this to get an email value from the URL get parameters:
$email = filter_input( INPUT_GET, ’email’, FILTER_SANITIZE_EMAIL );
Might as well get into the habit of grabbing variables this way.
Slim PHP and GET Parameters
I’m playing with Slim PHP as a framework for a RESTful API, and so far it’s great. Super easy to work with, but I do have one question I can’t find the answer to. How do I grab GET params from the URL in Slim PHP?
For example, if I wanted to use the following:
A case of the Mondays? Am I overthinking it? Thanks in advance!
9 Answers 9
You can do this very easily within the Slim framework, you can use:
$app here is a Slim instance.
Or if you want to be more specific
You would use it like so in a specific route
You can read the documentation on the request object http://docs.slimframework.com/request/variables/
For Slim 3/4 you need to use the method getQueryParams() on the PSR 7 Request object.
You can get the query parameters as an associative array on the Request object using getQueryParams().
I fixed my api to receive a json body OR url parameter like this.
This might not suit everyone but it worked for me.
In Slim 3.0 the following also works:
routes.php
user.php
Not sure much about Slim PHP, but if you want to access the parameters from a URL then you should use the:
You’ll find a bunch of blog posts on Google to solve this. You can also use the PHP function parse_url.
IF YOU WANT TO GET PARAMS WITH PARAM NAME
The params() method will first search PUT variables, then POST variables, then GET variables. If no variables are found, null is returned. If you only want to search for a specific type of variable, you can use these methods instead:
IF YOU WANT TO GET ALL PARAMETERS FROM REQUEST WITHOUT SPECIFYING PARAM NAME, YOU CAN GET ALL OF THEM INTO ARRAY IN FORMAT KEY => VALUE
$data will be an array that contains all fields from request as below