Avatar of pillmill
pillmill
 asked on

parse_url when url is a paramater?

Parse_url produces the wrong result the one of the parameters is itself a url:

For example,

parse_str($theurl['query'],$params);  

will lose all of the params after the url parameter, because the "&"
delimiting the parameters is not recognized.

How can I fix this? I need all the parameters from a URL REST query, when
one of the parameter values is itself a URL.  
PHPHTTP ProtocolWeb Development

Avatar of undefined
Last Comment
crazedsanity

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
HainKurt

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
OmniUnlimited

Also found this at php.net.  Don't know if it applies to you

URL's in the query string of a relative URL will cause a problem

fails:
/page.php?foo=bar&url=http://www.example.com

parses:
http://www.foo.com/page.php?foo=bar&url=http://www.example.com 
Ray Paseur

Here is the demonstration script proving that HainKurt is on the ball.
http://www.laprbass.com/RAY_temp_pillmill.php
Outputs:
http://www.hainkurt.com?a=1&b=2
http%3A%2F%2Fwww.hainkurt.com%3Fa%3D1%26b%3D2
http://www.hainkurt.com?a=1&b=2

Best to all, ~Ray
<?php // RAY_temp_pillmill.php
error_reporting(E_ALL);

$url = 'http://www.hainkurt.com?a=1&b=2';
$enc = urlencode($url);
$new = urldecode($enc);

echo "<pre>";
echo PHP_EOL . htmlentities($url);
echo PHP_EOL . htmlentities($enc);
echo PHP_EOL . htmlentities($new);

Open in new window

Sandeep Kothari

well you have to explode the parse string with & and before doing that you decode the parse string...

example code :

$theurl = "http://www.example.com?h=test&o=ok&urll=http://www.example.org&helo=bye&working=fine";

$param_arr = explode('&',$theurl);

print_r($param_arr);

Open in new window

Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
Ray Paseur

No points for this, please -- the question has already been answered.  The PHP URL functions are documented here:
http://php.net/manual/en/ref.url.php

These are the PHP functions that make it easy to get this right.
http://php.net/manual/en/function.urlencode.php
http://php.net/manual/en/function.urldecode.php

Best to all, ~Ray
crazedsanity

I concur with Ray_Paseur that HainKurt's answer is the one to use.  If the variable "query" were a URL such as "http://www.example.com?x=y&z=3", it would have to be passed through urlencode() in order to keep the values for $_GET['x'] and $_GET['z']; without that, it is simply not possible to be 100% sure that all the attached variables (except maybe the first) remain attached to it.

@kshna: please be respectful of other experts. There is no reason that the user asking the question should have to deal with such childish outbursts. Your fix does not actually fix the problem, but merely illustrates the problem: Ray_Paseur was saying "no points" for the post it was stated in, indicating that HainKurt's answer was the one that should be awarded points.