【问题标题】:how to force space to %20 in http_build_query?如何在 http_build_query 中将空间强制为 %20?
【发布时间】:2017-01-09 09:48:29
【问题描述】:

我遇到了一个问题,API 端点需要用%20 替换其中一个空格的参数之一。示例:

$params = array(
           'client_id' => self::$client_id,
           'scope' => 'api%20offline_access', //here I need the %20 sign
           'response_type' => 'code',
           'redirect_uri' => self::$redirect_uri
          );

如果我写'scope' => 'api offline_access',则在将空格传递给http_build_query() 函数时,它会被转换为+ 符号。

如果我保持上面的样子,标志会被更多标志“包裹”

我试图在功能手册中找到这个答案,但没有成功

【问题讨论】:

标签: php json http-headers


【解决方案1】:

正如@Paul 回复的那样:

http_build_query($params, null, null, PHP_QUERY_RFC3986);

根据enc_type参数的php doc。

【讨论】:

    【解决方案2】:

    修复上方评论

    http_build_query($params, null, '&', PHP_QUERY_RFC3986);
    

    如果第三个参数 (arg_separator) 更改为 null,则查询字符串粘在一起。

    file.php?query=firstpass=12345admin=root //no arg_separator
    
    file.php?query=first&pass=12345&admin=root //arg_separator
    

    【讨论】:

      猜你喜欢
      • 2021-01-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-18
      • 1970-01-01
      • 2010-12-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多