如何在http_build_query中将空间强制为%20?

Pav*_*cek 1 php json http-headers

我遇到一个问题,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
          );
Run Code Online (Sandbox Code Playgroud)

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

如果我像上面一样保持它,则该标志被“包裹”了更多的标志

我试图在功能手册中找到此答案,但是没有运气

Rem*_*let 6

正如@Paul回答:

http_build_query($params, null, null, PHP_QUERY_RFC3986);
Run Code Online (Sandbox Code Playgroud)

根据php doc的enc_type参数。