Search code examples
phpgeturlvariables

How can I efficiently add a GET parameter with either a ? or & in PHP?


I have to add a GET variable to a url. But the URL might already have GET variables. What's the most efficient way to add this new variable?

Example URLs:

http://domain.com/
http://domain.com/index.html?name=jones

I need to add: tag=xyz:

http://domain.com/?tag=xyz
http://domain.com/index.html?name=jones&tag=xyz

What's the most efficient way to know whether to prepend my string with a ? or &?

Here's a version of the function I have so far:

// where arrAdditions looks like array('key1'=>'value1','key2'=>'value2');
function appendUrlQueryString($url, $arrAdditions) {
    $arrQueryStrings = array();
    foreach ($arrAdditions as $k=>$v) {
        $arrQueryStrings[] = $k . '=' . $v;
    }
    $strAppend = implode('&',$arrQueryStrings);
    if (strpos($url, '?') !== false) {
        $url .= '&' . $strAppend;
    } else {
        $url = '?' . $strAppend;
    }
    return $url;
}

But, is simply looking for the ? in the existing url problematic? For example, is it possible that a url includes a ? but not actual queries, perhaps as an escaped character?


Solution

  • Take a look at PHP PECL's http_build_url. Said by the doc page:

    Build a URL.

    The parts of the second URL will be merged into the first according to the flags argument.

    Addition:

    If you don't have PECL installed, we can jump through some hoops. This approach is somewhat solid right up until we try to rebuild the new URL. Stock PHP (minus PECL) doesn't have a reverse of parse_url(). Making it harder, parse_url() removes some of the grammar from a URL in the resulting parts array so we have to put them back in when we reassemble. http_build_url() can take care of this for us, but if it were available, you wouldn't be reading this portion as it's what I originally recommended. Anyway, here's that code:

    <?php
    /**
     * addQueryParam - given a URL and some new params for its query string, return the modified URL
     * 
     * @see https://www.php.net/parse_url
     * @see https://www.php.net/parse_str
     * @throws Exception on bad input
     * @param STRING $url A parseable URL to add query params to
     * @param MIXED $input_query_vars - STRING of & separated pairs of = separated key values OR ASSOCIATIVE ARRAY of STRING keys => STRING values
     * @return STRING new URL
     */
    function addQueryParam ($url, $input_query_vars) {
        // Parse new parameters
        if (is_string($input_query_vars)) {
            parse_str($input_query_vars, $input_query_vars);
        }
    
        // Ensure array of parameters now available
        if (!is_array($input_query_vars)) {
            throw new Exception(__FUNCTION__ . ' expects associative array or query string as second parameter.');
        }
    
        // Break up given URL
        $url_parts = parse_url($url);
    
        // Test for proper URL parse
        if (!is_array($url_parts)) {
            throw new Exception(__FUNCTION__ . ' expects parseable URL as first parameter');
        }
    
        // Produce array of original query vars
        $original_query_vars = array();
        if (isset($url_parts['query']) && $url_parts['query'] !== '') {
            parse_str($url_parts['query'], $original_query_vars);
        }
    
        // Merge new params inot original
        $new_query_vars = array_merge($original_query_vars, $input_query_vars);
    
        // replace the original query string
        $url_parts['query'] = http_build_query($new_query_vars);
    
        // Put URL grammar back in place
        if (!empty($url_parts['scheme'])) {
            $url_parts['scheme'] .= '://';
        }
    
        if (!empty($url_parts['query'])) {
            $url_parts['query'] = '?' . $url_parts['query'];
        }
    
        if (!empty($url_parts['fragment'])) {
            $url_parts['fragment'] = '#' . $url_parts['fragment'];
        }
    
        // Put it all back together and return it
        return implode('', $url_parts);
    }
    
    // Your demo URLs
    $url1 = 'http://domain.com/';
    $url2 = 'http://domain.com/index.html?name=jones';
    
    //Some usage (I did this from CLI)
    echo $url1, "\n";
    echo addQueryParam($url1, 'tag=xyz'), "\n";
    echo addQueryParam($url1, array('tag' => 'xyz')), "\n";
    
    echo $url2, "\n";
    echo addQueryParam($url2, 'tag=xyz'), "\n";
    echo addQueryParam($url2, array('tag' => 'xyz')), "\n";
    echo addQueryParam($url2, array('name' => 'foo', 'tag' => 'xyz')), "\n";