gpt4 book ai didi

php - Python urlencode 不会用数组编码值

转载 作者:太空宇宙 更新时间:2023-11-04 05:00:17 25 4
gpt4 key购买 nike

我正在使用以下内容在我的 PHP 文件中创建 post 变量:

test=urllib2.urlopen(urllib2.Request('php.php',urllib.urlencode({'test':[1,2,3,4,5]}))).read()
print test

我希望它在 php.php 文件中创建一个 $_POST,它是一个变量数组和数组。

php.php:

print_r($_POST['test']);
echo is_array($_POST['test'])?"YES! THIS IS AN ARRAY":"NO! THIS IS A STRING";

不幸的是,它最多只能创建数组的“字符串”表示。

enter image description here

我已经尝试将 urlencode 参数设置为 True,就像我在类似问题中看到的建议一样:

urlencode({'test':[1,2,3,4,5]},True)

enter image description here

我也尝试使用 Python requests.post 并得到了类似的结果。

但是如您所见,它只输出数组中的最后一个数字。有没有办法在 Python 脚本中解决这个问题?

最佳答案

我找到了答案,而且很简单。

只需将 {'test':[1,2,3,4,5]} 键更改为 {'test[]':[1,2,3,4 ,5]}

注意我是如何添加 []

并确保你的参数中有 True

response=urllib2.urlopen(URL,urllib2.Request(urlencode({'test[]':[1,2,3,4,5]},True)))

如果您使用的是 requests.post 方法:

response=requests.post(URL,data={'test[]':[1,2,3,4,5]})

关于php - Python urlencode 不会用数组编码值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45901536/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com