python多维数据post给php,使用Python中的POST将数据发送到PHP

PHP code:

$data=$_POST['data'];

echo $data;

?>

When I do that, the HTML page that Python prints notifies me that PHP

did not receive any value in $data I.e:

Error in $name; undefined index

However, when I send the data as GET (http://localhost/mine.php?data=data) and change the PHP method from POST to GET ($data=$_GET['data']), the value is gotten and processed.

My main issue here is that it seems the value in data does not go through to PHP as I would have wanted to use POST. What could be wrong?

解决方案

Look at this python:

import urllib2, urllib

mydata=[('one','1'),('two','2')] #The first is the var name the second is the value

mydata=urllib.urlencode(mydata)

path='http://localhost/new.php' #the url you want to POST to

req=urllib2.Request(path, mydata)

req.add_header("Content-type", "application/x-www-form-urlencoded")

page=urllib2.urlopen(req).read()

print page

Almost everything was right there Look at line 2

heres the PHP:

echo $_POST['one'];

echo $_POST['two'];

?>

this should give you

1

2

Good luck and I hope this helps others

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值