【发布时间】:2015-07-28 01:15:20
【问题描述】:
好的,所以我一直在研究我拥有的代码太久了,我通过许多测试知道我必须面临超出我知识范围的问题。
简而言之,我正在尝试将从 Arduino(连接到我的笔记本电脑,并通过串行端口进行通信)接收到的数据发送到在我的笔记本电脑上运行的服务器。
我正在尝试使用请求库在 POST 请求中发送各种信息,如下所示:
import requests
import json
url = 'http://<usernames computer>.local/final/'
headers = {'Content-type': 'application/json'}
data = [
('state','true'),
('humidity', 45),
('temperature',76)
]
r = requests.post(url, data, headers = headers)
print r.text
此代码有效。我知道这一点,因为我在http://www.posttestserver.com/ 测试过它。所有数据均已正确发送。
但我正在尝试将其发送到如下所示的服务器端脚本:
<?php
$state = $_POST["state"];
$myfile = fopen("./data/current.json", "w") or die("Unable to open file!");
$txt = "$state";
fwrite($myfile, $txt);
fclose($myfile);
echo "\nThe current state is:\n $state\n";
?>
但是,当我运行代码时,我的脚本会吐出:
<br />
<b>Notice</b>: Undefined index: state in
<b>/Applications/XAMPP/xamppfiles/htdocs/final/index.php</b> on line
<b>2</b><br />
The current state is:
<This is where something should come back, but does not.>
可能出了什么问题?感谢您的帮助!
【问题讨论】: