在 Python 中使用 POST 将数据发送到 PHP

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4214231/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 14:47:58  来源:igfitidea点击:

Sending data using POST in Python to PHP

phppythonurllib2

提问by Crazywizard

PHP code:

PHP代码:

<?php
$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 $dataI.e:

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

Error in $name; undefined index

$name 错误;未定义索引

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.

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

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?

我在这里的主要问题是数据中的值似乎没有像我想要使用 POST 那样传递给 PHP。可能有什么问题?

回答by Ilian Iliev

import urllib
import urllib2

params = urllib.urlencode(parameters) # parameters is dicitonar
req = urllib2.Request(PP_URL, params) # PP_URL is the destionation URL
req.add_header("Content-type", "application/x-www-form-urlencoded")
response = urllib2.urlopen(req)

回答by TheBestJohn

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

几乎一切都在那里看第 2 行

heres the PHP:

继承人的PHP:

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

this should give you

这应该给你

1
2

Good luck and I hope this helps others

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

回答by user1767754

There are plenty articles out there which suggest using requestsrather then Urlliband urllib2. (Read References for more Information, the solution first)

有很多文章建议使用requests而不是Urlliburllib2。(阅读参考资料了解更多信息,解决方案优先)

Your Python-File (test.php):

你的 Python 文件(test.php):

import requests
userdata = {"firstname": "John", "lastname": "Doe", "password": "jdoe123"}
resp = requests.post('http://yourserver.de/test.php', params=userdata)

Your PHP-File:

您的 PHP 文件:

$firstname = htmlspecialchars($_GET["firstname"]);
$lastname = htmlspecialchars($_GET["lastname"]);
$password = htmlspecialchars($_GET["password"]);
echo "firstname: $firstname lastname: $lastname password: $password";

firstname: John lastname: Doe password: jdoe123

名字:John 姓氏:Doe 密码:jdoe123

References:

参考:

1) Good Article, why you should use requests

1)好文章,为什么你应该使用请求

2) What are the differences between the urllib, urllib2, and requests module?

2)urllib、urllib2、requests模块有什么区别?