mayersdesign
mayersdesign

Reputation: 5310

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.

Upvotes: 1

Views: 38

Answers (3)

Annapurna Agarwal
Annapurna Agarwal

Reputation: 61

Try like this

if (isset($_GET['url'])) {
  $query_string = $_SERVER['QUERY_STRING'];
  if (!empty($query_string)) {
     list($key, $xml_url) = explode('url=', $query_string); 
     echo $xml_url;
  }

}

Upvotes: 0

Gautam Rai
Gautam Rai

Reputation: 2505

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

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

Upvotes: 4

Adrian Baginski
Adrian Baginski

Reputation: 336

urlencode() is the way to go :)

Upvotes: 0

Related Questions