将多个 GET 参数合并为一个值
我不太确定我在寻找什么,但目前在我的系统中,我发送了一个像这样的长网址:
$name=1&option=2&field=4....
而且它很长。因此,如果我有一个值列表,例如:
- 名称
- 选项
- 字段,
我可以将它们放入一个字符串中,在该字符串中我可以在某些点处中断,例如通过斜杠或其他方式。
然后对字符串进行编码,使其完全随机,所以我只有一个字段要发送:
&data=JKHFGDKGLKJHFKDJHFKJDHFKHDF
最后我可以解码另一侧并分解。
是否有预先构建的函数可以执行此操作?
它是什么:
我正在向 PayPal 发送数据,但我有一些想要发送的自定义变量,现在由于某种原因我的 IPN 没有收到它们,不知道为什么,但如果我添加一个名为“自定义”的变量,它就会很好地发送到 IPN。所以我想如果我只是以随机格式发送一个名为“自定义”的文件然后解码?
Im not really sure what im looking for but currently in my system i send a long url like this:
$name=1&option=2&field=4....
And its quite long. So if i have a list of values like:
- name
- option
- field
can i put them into a string in which i can break at certain points eg by a slash or whatever.
And then encode the string so its completely random like, so i only have one field to send:
&data=JKHFGDKGLKJHFKDJHFKJDHFKHDF
Then finally i can decode the other side and break apart.
Is there a pre-built function to do this?
WHAT IT IS:
im sending data to paypal, but i have a few custom variables i wish to send, now for some reason my IPN isnt geting them, not sure why, but if i add one called custom it get to the IPN fine. So i thought if i just send one called custom in a random format and then decode?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以简单地对数据进行 Base64 编码。
输出:
要在 URL 中发送此内容,您必须再次对其进行编码(就像您必须对要在 URL 中发送的所有数据进行编码一样)
通过 JSON 的中间步骤可确保您的数据保持其结构,并且可以在接收端轻松解码。
缺点是:您的网址会更长。
You could simply Base64-encode your data.
output:
To send this in an URL, you must encode it once more (like you must encode all data you would send in a URL)
The intermediary step through JSON ensures your data will kep its structure and will be easily decodeable on the receiving side.
On the downside: Your URL will be longer.
使用 base64_encode 然后 base64_decode,这将解决您的问题。
Use base64_encode and then base64_decode, this will solve your problem.
如果你不知道你在寻找什么,我们就无法想象,哈哈。
无论如何,如果我找到了你,你可以这样做,考虑到你已经构建了你的字符串:
你可以将它作为单个参数传递:
除了
url_encode
你可以使用base64_encode
If you don't know what you are looking for we can't imagine that lol.
Anyway If i got you, you can do this, considering you have built your string:
You could pass it as a single param with:
Other than
url_encode
you can usebase64_encode
正如评论者 (Bobby) 所说 - 当您想要发送更大的数据集并防止您的 URL 变得难以管理的长或丑陋时,请考虑使用 POST。
GET 变量可以方便地为用户提供可以直接添加书签的页面,这在某些情况下是可取的,例如在已填充查询字符串和/或过滤器的搜索页面上,以便用户可以返回到搜索并查看结果。定期检查新结果,而无需重置所有选择。
如果您不需要此类功能,则 POST 变量会更好,您不需要对 URL 进行编码/解码,并且不能直接为它们添加书签(这在许多情况下也是可取的)。
不过,为了回答你最初的问题,如果你真的真的必须在 URL 上发送变量,并且你只想发送一个明显随机的字符串,我建议你编写一些自己的编码/解码函数(因为我假设练习的目的不是对其进行加密以防止篡改,只是为了使您的 URL 更友好)。如果变量可以是什么,这会更容易;如果变量可以是任何东西,这会更困难。
例如 - 如果您有以下变量和可能的设置:
通常,您会创建如下 URL:
http://www.mysite.com/page.php?var1=banana&var2=car&var3=green
如果将变量分配给数字(例如),则 var1, var2 var3 将为 1、2 或 3 - 那么您可以发送如下 URL:
http: //www.mysite.com/page.php?vars=213
在另一端将其分解为单个数字,并将它们转换回“香蕉”、“汽车”和“绿色”。
但说真的......我会先看看 POST ,除非有非常具体的原因为什么你会使用这种方法 - 我之前用它来缩短 URL 以使其在社交媒体和论坛上更容易共享。
As the commenter (Bobby) says - consider using POST when you want to send larger sets of data and prevent your URL from becoming unmanageably long or ugly.
GET variables are handy for providing the user with a page they can bookmark directly which is desirable in some cases, such as on a search page with a query string and/or filters already filled in, so that the user can return to a search and check for new results periodically without having to reset all of their choices.
POST variables are better if you don't need that sort of functionality, you don't need to encode/decode them for URLs and they can't be bookmarked directly (which is also desirable in many cases).
To answer your original question though, if you really, really had to send the variable(s) on the URL and you wanted to just send one apparently random string, I suggest writing a couple of encode/decode functions of your own (since I assume the object of the excercise is not to encrypt it against tampering, just to make your URLs friendlier). This will be all the easier if there are restrictions to what the variables can be, and more difficult if they can be absolutely anything.
For example - if you have the following vars and possible settings:
Normally, you'd make a URL like:
http://www.mysite.com/page.php?var1=banana&var2=car&var3=green
If you assign the variables to numbers (for example) so that var1, var2 and var3 would be 1, 2 or 3 - then you could send over a URL like:
http://www.mysite.com/page.php?vars=213
Break it down at the other end into single numbers and convert those back into 'banana', 'car', and 'green'.
But seriously.... I'd look at POST first unless there is very specific reason why you would use this sort of approach - i've used it before for shortening a URL to make it more sharable on social media and forums.