我的项目的范围是将某些值从 python 脚本传递到远程 php 脚本。
我有一个生成关联数组的 python 脚本。例如(已经 JSON 编码):
{"FRONT": "19.50", "RACK": "17.63", "REAR": "21.06", "ROOM": "15.6"}
我需要将此关联数组传递给我遵循本教程的远程 PHP 脚本:http: //nonstopblah.wordpress.com/2010/07/13/python-to-php-via-json/
我得到 200 的 HTTP 响应,但在 php 脚本中 POST 变量似乎为空
这是我的代码:
bulkData = json.dumps(temp, ensure_ascii = 'False')
# ensure_ascii is false as data is in unicode and not ascii encoding , use this if data is in any other encoding
print bulkData
print '\nHTTP Response'
headers = { "charset":"utf-8",
"Accept": "text/plain"}
conn = httplib.HTTPConnection(report_host)
postData = urllib.urlencode({'results':bulkData})
conn.request("POST", report_path, postData,headers)
response = conn.getresponse()
text = response.read()
print "Response status: ",response.status,"\n",text
conn.close()
这是 PHP 脚本:
if( isset($_POST['results']) )
{
$data = json_decode($_POST['results']);
print_r($data);
}
else
{
echo 'Nothing to listen.';
print_r($_POST);
}
这是我的python脚本的输出(带有远程响应):
{"FRONT": "20.44", "RACK": "18.88", "REAR": "21.25", "ROOM": "17.7"}
HTTP Response
Response status: 200
Nothing to listen.Array
(
)
有更聪明的方法吗?我在这里想念什么?
预先感谢您的友好回答。