phpPOST 数据到 Python

2018-12-24 16:46:46 +08:00
 lanqing

我用 python 语言

postdata = {'pic':[1,2,3]}
requests.post(url,data=postdata)

在后台可以看到这样的数据 pic:[1,2,3]

但是 php 他们传给我的时候(我不知道他们代码怎么写的,他们说只能这样传) 我接受这样的数据 pic[0]:1,pic[1]:2,pic[2]:3

我想问 php 代码该怎么写,我再后台能得到 pic:[1,2,3]这样的数据

2626 次点击
所在节点    Python
23 条回复
meik2333
2018-12-24 16:58:33 +08:00
首先你这个标题起的。。。

其次其实 PHP 传的是对的,Python 传的反而有问题。

如果你想要让 PHP 也传这样的数据,让他们拼接成字符串传过来吧。
lanqing
2018-12-24 17:01:55 +08:00
@meik2333 语言组织能力不行...
django 服务器接收请求
为啥说 python 传的有问题, python 传的在网络中传输的就是 pic=1;pic=2;pic=3
反而 php 在网络传输是 pic[0]=1;pic[1]=2;pic[2]=3
如果拼成成字符串 那我接受的不就是字符串了....
shuax
2018-12-24 17:02:09 +08:00
直接传 json 吧
mht
2018-12-24 17:03:59 +08:00
干脆让 php 直接给你传完整的 json 好了 php 用 form-data 的话数组就是会带有下标的吧
araraloren
2018-12-24 17:04:27 +08:00
交流要先统一格式。。
lanqing
2018-12-24 17:05:51 +08:00
@mht
@shuax
其实传什么给我我都无所谓...解析一下就行 只是想知道 php 有没有这个功能能满足我的要求,没有就算了= =,
markgor
2018-12-24 17:16:25 +08:00
pic:[1,2,3] 这种格式真的没见过
如果是 JSON 格式应该是:'{"pic":[1,2,3]}' 这样吧?
PHP 传的 pic[0]:1,pic[1]:2,pic[2]:3 是典型的 form 形式提交给你,
如果通过设置 JSON 方式提交给你,格式就是上面说到的'{"pic":[1,2,3]}'
然后你通过 JSON 格式化下就能得到对应的值了( PY 没用过不清楚)。
ninestep
2018-12-24 17:16:40 +08:00
1. 绝对能实现,这么个小需求说不能实现绝对是扯淡。
2. 你 python 处理现在的数据成为你要的格式也绝对能实现,不能实现也是扯淡。
3. 第一次在 post 请求中见到直接传语言的数据结构而不是 json,xml 等数据结构的
lanqing
2018-12-24 17:20:38 +08:00
@markgor 你应该不知道 python 语言 pic:[1,2,3]=>标示 key:value , 然后 requests 库会自动转成相应的网络数据发送到服务器,完全没问题 . pic[0]:1,pic[1]:2,pic[2]:3 这个在我理解中就是三个 key:value key=>pic[0],pic[1],pic[2],如果这样传值 ,跟普通的 name:xxx,age:qqq 又有啥区别呢
lanqing
2018-12-24 17:22:42 +08:00
@ninestep
1. 你实现
2. 我没说不能实现,只要规定了规则,你给我传什么,我都可以给你转回来
3. 我用了 requests 库,没见过你可以查一下
Vegetable
2018-12-24 17:23:07 +08:00
1.你这个方式发的,传出去的是 form 格式,请求 body 是这样的 pic=1&pic=2&pic=3
2.django 里接收的时候,应该是需要用 request.GET.getlist("pic")才能取到你说的值.
3.php 发出来是什么样我不知道,但是你可以让他们拼接成 pic=1&pic=2&pic=3 这种形式发给你,但是也是不推荐的.还是建议使用 application/json 的形式发.
lanqing
2018-12-24 17:24:29 +08:00
@Vegetable 你说的前两点对的,关于第三点,我个人觉得 php 实现不了,我才过来问的
lanqing
2018-12-24 17:29:25 +08:00
我觉得我说的非常清楚了... 并不是我不会接收数据,我只想想问 php 有没有方法在 post 的时候 ,网络 body 是 pic=1&pic=2&pic=3 这样的, 这样我再 django 中就可以得到一个 list.
我觉得我同事写的方式 post 数据是 pic[0]=1&pic[1]=2&pic[2]=3,这样我在 django 中得到的就是 pic[0],pic[1]分开的,而不是 list
Vegetable
2018-12-24 17:34:34 +08:00
@lanqing

http://php.net/manual/en/function.curl-setopt.php

CURLOPT_POSTFIELDS
The full data to post in a HTTP "POST" operation. To post a file, prepend a filename with @ and use the full path. The filetype can be explicitly specified by following the filename with the type in the format ';type=mimetype'.

***** This parameter can either be passed as a urlencoded string like 'para1=val1&para2=val2&...' *****

or as an array with the field name as key and field data as value. If value is an array, the Content-Type header will be set to multipart/form-data. As of PHP 5.2.0, value must be an array if files are passed to this option with the @ prefix. As of PHP 5.5.0, the @ prefix is deprecated and files can be sent using CURLFile. The @ prefix can be disabled for safe passing of values beginning with @ by setting the CURLOPT_SAFE_UPLOAD option to TRUE.

怎么会不行呢?我都不会写 PHP,也能找到怎么写啊
lanqing
2018-12-24 17:50:29 +08:00
@Vegetable 修改请求头么.multipart/form-data 我试了下还是不行..
markgor
2018-12-24 18:15:16 +08:00
@lanqing
Py 的我真的不清楚,

但想确认一点,如果是 JSON 的数据的话,你是不是可以转为 LIST ?
如果是的话,那 PHP 直接提交 JSON 给你就可以啦?
我只试过 PHP 提交 JSON 去.NET 和 JAVA 都可以。

附上 JSON 代码:
<?php
$arr = array(
'pic'=>array(1,2,3,4)
);
$data_string = json_encode($arr);
$ch = curl_init(你的地址);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS,$data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string)
));

$result = curl_exec($ch);
if (curl_errno($ch)) {
print curl_error($ch);
}
curl_close($ch);
echo $result;
lanqing
2018-12-24 20:23:17 +08:00
@markgor json 肯定可以的
learnshare
2018-12-24 20:25:03 +08:00
JSON 就好了
Bryan0Z
2018-12-24 22:41:10 +08:00
如果只是需要两种语言通信,可以考虑 GRPC
xiangyuecn
2018-12-24 22:55:41 +08:00
我觉得还是慎用数组形式的这种参数,简单字符串 key [唯一] 、字符串 value 多好,最怕这种数组形式的,对整个请求解析出来不是简单的字典也不是简单的数组,一个字烦,两个字麻烦,复杂的用 json 字符串 这个世界清静了

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/520528

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX