-1

大家晚上好。

首先,我为这个问题的长度道歉。我想彻底,并确保所有信息都与我的意图一起出现。

我目前正在寻找一种简单的基于 Web 的方法来从我的一个桌面应用程序中收集统计信息。这样做的主要目的是进行故障排除,并通过更新给最终用户提供更多相关的修复和功能添加。

细节

  • 我将使用 PHP 5.2.12,并利用$_GET从 URL 中获取参数并将它们分配给变量。
  • 然后将这些变量json_encode写入文件(称为“statsfile”或“stats 文件”)。
  • 其中一个参数/变量将是某种 GUID。这个 GUID 将由我的应用程序根据从硬件 ID 创建的单向哈希生成(尽管在代码示例中,这还没有表示出来)。

问题

我面临的问题与在我的 JSON 统计文件中查找特定的 GUID 有关。

这就是我想做的;

  • 1) 在 statsfile 中找到提交的 GUID
  • 2a) 如果不存在,则向数组添加新索引并填充里面的数据
  • 2b)如果确实存在,则用提交的数据覆盖数组内的现有数据

重要的是,最后我留下了一个有效的 JSON 统计文件。脚本相对安全也很重要。

布局和代码

我的 JSON 的布局如下;

[
    {
        "guid": "spritchard",
        "api": "apichoice",
        "build": "2200",
        "temp1": "1",
        "temp2": "2",
        "temp3": "3"
    },
    {
        "guid": "helloworld",
        "api": "someapi",
        "build": "3500",
        "temp1": "4",
        "temp2": "5",
        "temp3": "6"
    }
]

我正在使用的 PHP 代码如下;

<?php
$apikey=$_GET["apikey"];
if ($apikey=="apikey8634215")
  {
    $gpuapi=$_GET["api"];
    $buildno=$_GET["build"];
    $temp1=$_GET["temp1"];
    $temp2=$_GET["temp2"];
    $temp3=$_GET["temp3"];
    $guid=$_GET["guid"];

    $statsfile = "./api/application/StatsFile.json"; // Assign the stats file
    $fh = fopen($statsfile, 'a') // 'a' for append, 'w' for write
          or die("Stats File Unavailable");
    $sdata = array('guid' => $guid, 'api' => $gpuapi, 'build' => $buildno, 'temp1' => $temp1, 'temp2' => $temp2, 'temp3' => $temp3);
    fwrite($fh, json_encode($sdata));
    fclose($fh);
    echo json_encode($sdata);

    // reopen stats file to encode data as it should be
//    $cdata = fopen($statsfile, 'r') or die("Stats File Unavailable");
//    $encodedata = fread($cdata, filesize($statsfile));
//    $decodedata = json_decode($encodedata);
//    fclose($cdata);
//    $fh = fopen($statsfile, 'w') or die("Stats File Unavailable");
//    fwrite($fh, json_encode($encodedata));
//    fclose($fh);
  }
else
  {
    die("No Such Usage");
  }
?>

您可以看到我已经$apikey习惯检查是否存在特定参数。如果不是,那么整个脚本就会死掉。目前这只是一个占位符设计,以在我开发系统时从基本层面防止滥用,我将在一个单独的问题中研究更合适的解决方案。

被注释掉的区域是我试图用新数据重写文件并使其成为有效 JSON 的代码。我不知道如何在数组中找到正确的 GUID,但我也在努力弄清楚如何重写数据以便将其正确编码为 JSON。取消注释该代码的最终结果是 JSON 中有很多斜杠,并且 JSON 本身无效。此外,我不相信我在这里展示的代码是安全的。无论如何,我可以使用一种POST方法,但目前,找出如何根据它的 GUID 更新特定索引是个问题。

因此,为了澄清我的问题,我将如何从 JSON statsfile 中查找特定的 GUID、更新数据(如果存在)或使用提交的数据添加新的数组索引?

4

1 回答 1

1
$apikey=$_GET["apikey"];
if ($apikey=="apikey8634215") {
    $newObject = new stdClass();
    $newObject->guid = $_GET["guid"];
    $newObject->api = $_GET["api"];
    $newObject->build = $_GET["build"];
    $newObject->temp1 = $_GET["temp1"];
    $newObject->temp2 = $_GET["temp2"];
    $newObject->temp3 = $_GET["temp3"];
    $statsFile = "./api/application/StatsFile.json";
    $stats = file_exists($statsFile)
        ? json_decode(file_get_contents($statsFile))
        : array()
    ;
    $found = false;
    foreach ($stats as $key => $statItem) {
        if ($statItem->guid == $newObject->guid) {
            $stats[$key] = $newObject;
            $found = true;
            break;
        }
    }
    if (!found) {
        $stats[] = $newObject;
    }
    if (file_put_contents($statsFile, json_encode($stats)) === false) {
        die("Could not save stats");
    }
} else {
    die("No Such Usage");
}

此代码缺少获取参数存在检查,您应该以某种方式验证输入。

于 2012-11-30T22:59:21.067 回答