php用参数中的空格编码完整的URL [英] php encode full url with spaces in arguments

查看:199
本文介绍了php用参数中的空格编码完整的URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个看起来像这样的网址

I have a url that looks like that

http://example.com/index.php?id=111&title=word1 word2 word3

在使用curl请求url之前,我需要对空格进行编码.

I need to encode the spaces before requesting the url with curl.

但是如何对网址的正确部分进行urlencode.

But how to urlencode the right parts of the url.

urlencoderawurlencode编码https问号等的斜杠.

urlencode and rawurlencode encode the slashes of https the question mark etc.

推荐答案

您可以使用 parse_url()将URL分成各自的部分.在parse_url返回的数组中的query元素上使用urlencode(),然后使用

You can use parse_url() to split the URL into its respective pieces. Use urlencode() on the query element from the array that parse_url returns, then put it back together using http_build_url.

注意:http_build_url需要PECL pecl_http> = 0.21.0

NOTE: http_build_url requires PECL pecl_http >= 0.21.0

这篇关于php用参数中的空格编码完整的URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆