我有一个看起来像那样的 url
http://example.com/index.php?id=111&title=word1 word2 word3
在使用 curl 请求 url 之前,我需要对空格进行编码。
但是如何对 url 的正确部分进行 urlencode。
urlencode
和rawurlencode
对https
的斜线、问号等进行编码
最佳答案
您可以使用 parse_url()将 URL 拆分成各自的部分。在 parse_url
返回的数组中的 query
元素上使用 urlencode()
,然后使用 http_build_url 将其放回原处.
注意:http_build_url
需要 PECL pecl_http >= 0.21.0
关于php用参数中的空格编码完整的url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9922407/