gpt4 book ai didi

php - 通过 python 将 JSON 发送到外部 PHP 监听器

转载 作者:太空宇宙 更新时间:2023-11-03 18:00:15 25 4
gpt4 key购买 nike

我遇到的问题有点奇怪,但我会尽力解释。

目前,我运行一系列脚本来生成一个 JSON 文件,该文件存储有关服务器机房的详细信息。然后,该 JSON 文件被传递到 PHP 监听器,该监听器通过解析 JSON 并提取信息来填充数据库。

当使用 mozilla 添加“Poster”时,JSON 可以很好地发送到数据库,但是每当我从虚拟机运行它并尝试自动化它时,我都会收到此错误 -

fatal error :只有变量可以通过引用传递

我也遇到了很多这样的错误

98 行的 C:\xampp\htdocs\globalHEC\php_http_request\hecdashboard.php 中存在非法字符串偏移“acronym”

发送python方法:

def send_json_results(result_dict, server_ip, server_port):
'''Send the json file usinf http to the listening monitoring server'''
url="http://%s:%s/globalHEC/php_http_request/hecdashboard.php?id=ni100&type=json" %(server_ip, server_port)
data=json.dumps(result_dict)
req = urllib2.Request(url, data, {'Content-Type': 'application/json'})
f = urllib2.urlopen(req)
resp = f.read()
print "Results sent to %s with response: %s" % (server_ip, resp)

PHP 监听器代码段:

$locres = $mysqli->query("SELECT loc1.Id FROM `location` AS loc1 WHERE loc1.Timestamp = (SELECT MAX(loc2.Timestamp) FROM location AS loc2 WHERE loc2.Id = loc1.Id LIMIT 1) AND
loc1.Id = '".$jsonArray_new["acronym"]."' AND
loc1.Continent= '".$jsonArray_new["continent"]."' AND
loc1.Country = '".$jsonArray_new["country"]."' AND
loc1.City = '".$jsonArray_new["city"]."' AND
loc1.Latitude = '".$jsonArray_new["latitude"]."' AND
loc1.Longitude = '".$jsonArray_new["longitude"]."' AND
loc1.CellVersion = '".$jsonArray_new["cell_version"]."' AND
loc1.NumberOfCells = '".$jsonArray_new["number_of_cells"]."' LIMIT 1;");

//any changes or fresh Location, so add it
if ($locres->num_rows == 0) {
if ($stmt = $mysqli->prepare('INSERT INTO location (Id, Continent, Country, City, Latitude, Longitude, CellVersion, NumberOfCells, Timestamp) VALUES (?,?,?,?,?,?,?,?,?)')) {
$res = $stmt->bind_param("ssssddsis", $jsonArray_new['acronym'], $jsonArray_new['continent'], $jsonArray_new['country'], $jsonArray_new['city'], $jsonArray_new['latitude'], $jsonArray_new['longitude'], $jsonArray_new['cell_version'], $jsonArray_new['number_of_cells'], $jsonArray_new['timestamp']);
$finishings += (int)$stmt->execute();
$finishingQuota++;
}
}

jsonArray_new 初始化如下:

$file_content = file_get_contents('php://input');
echo "try to create file: ".$folder.$location.".json";
$numbytes = file_put_contents($folder.$location.".json", $file_content) OR EXIT("ERROR: Creation of ".$location.".json was not possible.");
print "$numbytes bytes written\n";
//}
$jsonString_new = $file_content;

$jsonArray_new = json_decode($jsonString_new, true);

最佳答案

您可能需要检查您的 json 是否已正确创建。在 json_decode 调用之后,生成的数组可能由嵌套数组组成,例如

$jsonArray_new = array (
array(
'your JSON data here as key=>value pairs'
),
);

因此禁止您通过键获取数组值。

在使用解码数据之前,我建议进行简单的 empty() 检查,因为解码可能会失败,并且 $jsonArray_new 可能会将 false 作为值。

旁注:您的代码需要重构。潜在的故障点太多。

关于php - 通过 python 将 JSON 发送到外部 PHP 监听器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27796280/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com