简体   繁体   English

PHP urlencode一个URL发布字段

[英]PHP urlencode a URL post field

I am using curl to call an API POST and pass 15 post fields, one being an actual wbsite URL. 我正在使用curl调用API POST并传递15个post字段,其中一个是实际的wbsite URL。 For example 例如

https://www.test.com?URL=https://www.myurl.com;&Name=John https://www.test.com?URL=https://www.myurl.com;&Name=John

Using urlencode converts the characters in the URL post field and the API receiving the POST call does not recognize it as an actual URL. 使用urlencode会转换URL post字段中的字符,并且接收POST调用的API不会将其识别为实际URL。 Is there any way to keep the URL string as is when positing? 有什么方法可以在放置时保持URL字符串不变吗?

If you are using GET query arguments, you need to urlencode the value, then decode it on the backend: 如果使用的是GET查询参数,则需要对值进行urlencode,然后在后端对其进行解码:

https://www.test.com?URL=https%3A%2F%2Fwww.myurl.com

And then: 接着:

urldecode($_GET['url'])

For POST , the same is true for application/x-www-form-urlencoded , but doesn't matter for application/json . 对于POSTapplication/x-www-form-urlencoded ,但对于application/json无关紧要。

Also, just a heads up, you have an extra ; 另外,只要抬起头,您就有了额外的钱; in your URL, which would make it invalid if you included it. 在您的网址中,如果包含它,将使其无效。

Edit: 编辑:

I will additionally add, that in general, it's good practice to always urldecode values received from query arugments/form data. 我将另外补充一点,在一般情况下,这是很好的做法,从查询arugments /表单数据接收总是 urldecode值。 The general thought here being that, while you may not want urlencoded data, that's the way the web is built - You should build your code in a way that it's up to the client. 这里的总体思路是,虽然你可能不希望 urlencoded的数据,这就是网络的构建方式-你应该建立自己的代码的方式,它是由客户端。 This is considered an important step in data sanitation, and should always happen before validation. 这被认为是数据卫生的重要步骤,应始终在验证之前进行。

您可以使用php内置方法来解码url编码的数据。

$actualUrl = urldecode ($_GET ['url']);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM