dotaer1993 2014-03-20 15:44 采纳率: 100%
浏览 74
已采纳

如何有效地添加GET参数? 或者用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?

  • 写回答

3条回答 默认 最新

  • duanqi5333 2014-03-20 15:48
    关注

    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 http://us1.php.net/parse_url
     * @see http://us1.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, "
    ";
    echo addQueryParam($url1, 'tag=xyz'), "
    ";
    echo addQueryParam($url1, array('tag' => 'xyz')), "
    ";
    
    echo $url2, "
    ";
    echo addQueryParam($url2, 'tag=xyz'), "
    ";
    echo addQueryParam($url2, array('tag' => 'xyz')), "
    ";
    echo addQueryParam($url2, array('name' => 'foo', 'tag' => 'xyz')), "
    ";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算