【发布时间】:2014-06-20 04:52:43
【问题描述】:
我正在创建一个网站,我正在使用 HTML 创建一个表单供用户输入信息。一旦用户输入此信息,PHP 程序就会读取输入并将其发送到 Python 程序。这个 python 程序创建一个用户数据库,然后返回用户的唯一 ID 号。但是,我不太确定如何通过 PHP 将 Python 中的值返回到 HTML。
有什么建议吗?
谢谢!
编辑(我正在添加一些代码):
HTML 格式如下。可以看到它调用了example.php。
<section id="content">
<div class="shady bott-27"></div>
<div class="inner clearfix">
<div class="inner-t">
<div class="col1">
<div class="heading">
<h3 id="Contact" href="#Contact"><strong><span style="color:#87CEEB">Preliminary Information</span></strong></h3>
</div>
<br>
</div>
<div class="col1-2">
<h4></h4>
<form id="contact" method="post" action="example.php">
<div>
<label>Something*</label> <input id="something" type="text" name="something" />
</div>
PHP 代码是:
$something = $_REQUEST["something"];
$command = "python /var/www/html/pythonfile.py";
$command .= " \"$something\" 2>&1";
$pid = popen( $command,"r");
while( !feof( $pid ) )
{
echo fread($pid, 256);
flush();
ob_flush();
usleep(100000);
}
pclose($pid);
?>
最后是 Python 代码:
def examplepython(something):
#this function creates a new vendor
appID = ....
apiKey = ....
url = ...
ps2 = {"field_1": something}
headers = {
"Content-Type": "application/json",
"X-Knack-Application-Id": appID,
"X-Knack-REST-API-Key": apiKey
}
datas = json.dumps(ps2)
req = urllib2.Request(url, datas, headers)
try:
f = urllib2.urlopen(req)
except urllib2.HTTPError, ex:
if ex.fp:
extra = ex.fp.read()
print "%s" % extra
else:
raise
data = json.loads(f.read())
identification = data["id"]
print "Your ID number is: " + identification
【问题讨论】:
-
为什么会涉及 PHP?您需要发布更多信息和代码
-
好吧,我的部分 html 代码是:
-
您提出这个问题的方式含糊不清,因此很难提供任何有意义的详细解决方案。你可以说得更详细点吗?你试过什么了?向我们展示您的代码。您是否遇到错误?
-
我只是做了一些编辑并添加了一些代码。感谢您的建议。