0

In php, if I wanted to parse a URL, such as www.site.com/index.php?foo=bar, I can do with the _POST variable. I can retrieve bar by _POST['foo'].

But what if my URL is something like www.site.com/index.php?foo&bar. i.e., I don't have the arguments as key-value pairs as in the first case. How to parse this in php?

shar
  • 1,682
  • 1
  • 14
  • 22
  • 3
    have you tried `var_dump`'ing `$_POST` before asking? What did you find? Also, what makes you think that `/index.php?foo=bar` would populate `$_POST` in the first place? POST data is not submitted in the URL but in the Request body. The arguments submitted through the URL are in `$_GET`. – Gordon Feb 16 '14 at 11:38
  • Gordon, First of all, I am new to PHP. I am learning it only now as I progress with my work. I tried paser_url in php. Even that is somethign similar to _POST. From what you have said, I searched var_dump now. Thanks. – shar Feb 16 '14 at 11:41
  • Gordon's remark wasn't about using var_dump, but rather that for GET requests (ie. with arguments in the URI), you should use the $_GET superglobal array. For POST requests (ie. posting a form with method="post") you would use the $_POST superglobal array instead. – Tularis Feb 16 '14 at 11:45
  • I think he's trying to parse a URL other than the current one. – AKS Feb 16 '14 at 11:47

2 Answers2

2

Use parse_url.

Quoted from php.net:

This function parses a URL and returns an associative array containing any of the various components of the URL that are present.

This function is not meant to validate the given URL, it only breaks it up into the above listed parts. Partial URLs are also accepted, parse_url() tries its best to parse them correctly.

Then, use parse_str function to parse the query parameters part.

Parses str as if it were the query string passed via a URL and sets variables in the current scope. To get the current QUERY_STRING, you may use the variable $_SERVER['QUERY_STRING']. Also, you may want to read the section on variables from external sources. The magic_quotes_gpc setting affects the output of this function, as parse_str() uses the same mechanism that PHP uses to populate the $_GET, $_POST, etc. variables.

<?php 
  $parts = parse_url('www.site.com/index.php?foo&bar');
  print_r($parts);
  // Array ( [path] => www.site.com/index.php [query] => foo&bar )


  parse_str ($parts['query'], $query);
  print_r($query);
  // Array ( [foo] => [bar] => ) 

 // Alternately you can do this in a single shot like below:
 parse_str(parse_url('www.site.com/index.php?foo&bar', PHP_URL_QUERY));

?>
AKS
  • 4,557
  • 2
  • 27
  • 48
  • I thought it was a great answer. Too lazy to try to find it on php.net directly. :) +1 from me. – thexfactor May 09 '14 at 14:02
  • Edit: perhaps you should have mentioned that the "alternative" version sets variables in the current scope. If you add $query to parse_str then the effect is the same as the first example. – thexfactor May 09 '14 at 14:12
1

It's a misconception that requesting a URL like

http://www.example.com/index.php?foo=bar

would give you bar in $_POST['bar']. Url parameters will populate $_GET. Anything that's supposed to show up in $_POST has to be submitted in the Request body. See How are parameters sent in an HTTP POST request? for some details.

With that clarified, empty URL parameters are not a problem at all. A URL like

http://www.example.com/index.php?foo&bar

will populate $_GET['foo'] and $_GET['bar'] with empty values.

Community
  • 1
  • 1
Gordon
  • 296,205
  • 68
  • 508
  • 534