我有一个Python插件,我需要连接到远程MySQL服务器。这个插件将在多个生产设备上,源是开放的,所以我不希望存储在那里的数据库信息。所以我试图使用PHP Web服务来获取数据,并使用post请求来访问它。当python脚本运行时,它会给出一个错误,并说No JSON对象可以被解码。
这是PHP代码(SQL连接凭据编辑):
$con = mysqli_connect('url', 'user', 'pass', 'db') or die('Connection Failed');
$mac = $_POST['mac'];
$result = mysqli_query($con, "SELECT 1 FROM `Boxes` WHERE `MAC` = '$mac'");
$rows = $result->num_rows;
if ($rows != 0) {
$response['Result'] = "Registered";
} else {
$response['Result'] = "Not Registered";
}
json_encode($response);
mysqli_close($con);
这是Python代码:
params = {'mac': mac}
r = requests.post("http://lastboxusa.com/php/LCheckReg.php", data=params)
data = r.json()
return data['Result'] == "Registered"
最后,这里是完整的堆栈跟踪:
Error Type: <type 'exceptions.ValueError'>
Error Contents: No JSON object could be decoded
Traceback (most recent call last):
File "C:\Users\ncben\AppData\Roaming\Kodi\addons\service.lastboxmanagement\addon.py", line 94, in <module> if checkreg(mac):
File "C:\Users\ncben\AppData\Roaming\Kodi\addons\service.lastboxmanagement\addon.py", line 35, in checkreg data = r.json()
File "C:\Users\ncben\AppData\Roaming\Kodi\addons\script.module.requests\lib\requests\models.py", line 850, in json return complexjson.loads(self.text, **kwargs)
File "C:\Program Files (x86)\Kodi\system\python\Lib\json\__init__.py", line 339, in loads return _default_decoder.decode(s)
File "C:\Program Files (x86)\Kodi\system\python\Lib\json\decoder.py", line 364, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Program Files (x86)\Kodi\system\python\Lib\json\decoder.py", line 382, in raw_decode raise ValueError("No JSON object could be decoded")
我是非常新的PHP所以(我希望)它在我的PHP中可能是一些愚蠢的错误。任何帮助将不胜感激。
答案 0 :(得分:0)
json_encode($response);
行应为echo json_encode($response);
但更重要的是,密码应该输入正确。