使用Python中的POST将数据发送到PHP [英] Sending data using POST in Python to PHP

查看:305
本文介绍了使用Python中的POST将数据发送到PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

PHP代码:

<?php
$data=$_POST['data'];
echo $data;
?>

当我这样做时,Python打印的HTML页面会通知我PHP 在$data中没有收到任何值,即:

When I do that, the HTML page that Python prints notifies me that PHP did not receive any value in $data I.e:

$ name中的错误;未定义的索引

Error in $name; undefined index

但是,当我将数据作为GET(http://localhost/mine.php?data=data)发送并将PHP方法从POST更改为GET($data=$_GET['data'])时,将获取并处理该值.

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.

我在这里的主要问题是,数据的价值似乎并没有传递给PHP,就像我本想使用POST一样.有什么问题吗?

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?

推荐答案

看看这个python:

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

此处是PHP:

<?php
echo $_POST['one'];
echo $_POST['two'];
?>

这应该给你

1
2

祝你好运,我希望这对其他人有帮助

Good luck and I hope this helps others

这篇关于使用Python中的POST将数据发送到PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆