我有一个看起来像这样的网址
http://example.com/index.php?id=111&title=word1 word2 word3
在使用curl请求url之前,我需要对空格进行编码。
但是如何对网址的正确部分进行urlencode。
[urlencode
和rawurlencode
编码https
问号等的斜杠。
您可以使用parse_url()将URL分成各个部分。使用urlencode()
返回的数组中query
元素上的parse_url
,然后使用http_build_url将其放回原处。
注意:http_build_url
需要PECL pecl_http> = 0.21.0
用“ +”代替空白。
尝试一下此功能,我做了一点测试,效果很好https://gist.github.com/volandku/8000c7deb654df93ebd181e40b52be97