Python 到 PHP 的函数
有谁知道如何将此函数从Python转换为PHP?
我一直在解决这个问题,但 Python 代码中有些东西我无法弄清楚。
该函数在以下上下文中使用: http://www.dmcloud.net/doc/api/general.html#authentication谢谢
def normalize(input=None):
output = ''
if type(input) in (list, tuple):
for element in input:
if type(element) in (dict, list, tuple):
element = normalize(element)
output += str(element)
elif type(input) is dict:
keys = input.keys()
keys.sort()
for key in keys:
element = input[key]
if type(element) in (dict, list, tuple):
element = normalize(element)
output += '%s%s' % (key, element)
else:
output = str(input)
return output
!
Does anyone know how to convert this function from Python to PHP?
I have been around it but there is things in Python code I can't figure out.
This function is used in the follow context:
http://www.dmcloud.net/doc/api/general.html#authentication
def normalize(input=None):
output = ''
if type(input) in (list, tuple):
for element in input:
if type(element) in (dict, list, tuple):
element = normalize(element)
output += str(element)
elif type(input) is dict:
keys = input.keys()
keys.sort()
for key in keys:
element = input[key]
if type(element) in (dict, list, tuple):
element = normalize(element)
output += '%s%s' % (key, element)
else:
output = str(input)
return output
Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
php 在字典和元组/列表之间没有区别,因此:
值得注意的是,这种序列化很糟糕,因为它无法区分整数和数字。您确实应该使用 JSON (在 php 以及 Python),这是一种独立于语言、人类可读的序列化格式。
不应使用此字符串进行身份验证,而应简单地在 JSON 序列化上使用 哈希。
php has no distinction between dictionaries and tuples/lists, so:
Notably, this serialization sucks, since it cannot distinguish integers from numbers. YOu should really be using JSON (in php as well as Python), which is a language-independent, human-readable serialization format.
Instead of using this string for authentication, one should simply use a hash over the JSON serialization.