Search code examples
phpurlget

Retrieving whole URL, even with ampersands, using _GET


I made a quick script to retrieve and parse XML. It's simply for internal use and I though that appending the feed URL to the script address would be a convenient way to initialize the script...

www.example.com/feed_analyzer.php?url=www.example.com/an_xml_feed.xml

Then I simply grab the URL...

if (isset($_GET['url'])) {
  $xml_url = $_GET['url']
}

...retrieve the file at $xml_url, parse etc.

All was fine until this URL came along with pesky parameters:

www.example.com/an_xml_feed.xml?foo=bar&rice=chips

That of course left me with the URL "www.example.com/an_xml_feed.xml"

I have managed to "patch back together" the whole URL using this clunky code:

if (isset($_GET['url'])) {
  foreach($_GET as $key => $value){
   $got .= "&".$key."=".$value;
  }
  $xml_url = ltrim($got,'&url=');
}

Can someone please suggest a more elegant approach.


Solution

  • You can directly use this to get the whole query url:

    ltrim($_SERVER['QUERY_STRING'], 'url=');