php - 可执行文件有更多数据要发送,但不知道如何向可执行文件发送信号,表明已发送的数据已处理并准备好下一个 block

标签 php mysql function url get

全部

这是我在这里的第一个问题,所以如果我犯了错误,请告诉我。我想尽我所能学习。

话虽如此,这就是我的困境。

我正在尝试修改一些 php,以便与 Microsoft Flight Simulator 一起用作 ACARS 的程序可以将日志文件发送到我的代码并将该数据写入 MySQL 数据库。我提到的程序将数据分解为 1Kb 的 block ,发送第一位并等待代码响应 OK beofre 发送下一位,依此类推,直到所有数据都被检索并写入文件 (txt) 并写入一个数据库。这一切都很好地未经修改。这是未修改的代码;

@define ("MYSQL_CONNECT_INCLUDE", "connect_db.php");        // MySQL     database connection (a sample file is included)
@define ("REPORT_FILE_URL", "http://dcascreenshots.net/fsacars/logs/");             // URL where the complete FSAcars reports will be stored
@define ("REPORT_FILE_PATH", "/home/axeman65/public_html/fsacars/logs/");               // Folder where the complete FSAcars reports will be stored
@define ("ERROR_LOG_PATH", "/home/axeman65/public_html/fsacars/error.log");                 // Folder and filename where the error log is located

/*
 * Error messages */
@define ("ERROR_OPENING_REPORT_FILE","Error opening report file");
@define ("ERROR_WRITING_REPORT_FILE","Error writing report file");
@define ("PILOT_NOT_FOUND","Pilot not found");
@define ("ERROR_IN_PILOT_QUERY","Pilot query error");
@define ("ERROR_INSERTING_PIREP","Error inserting report");



function CheckFSAcarsInfo() {   
    // Verify input
    if (!isset($_GET['pilot'])) { return 0; }

    // Request is not empty
    return 1;
}

function GetFSAcarsInfo() {
/* ************************************************************************************************
   @GetFSAcarsInfo
   Receives inputs sent by FSAcars program and returns an array containing that information

   Inputs: N/A
   Outputs: string array 
   ************************************************************************************************ */

// DO NOT EDIT THIS FUNCTION - THIS FIELDS ARE SENT BY FSACARS
$fsacars_pirep = array (
    "pilot" => $_GET['pilot'],
    "date" => $_GET['date'],
    "time" => $_GET['time'],
    "callsign" => $_GET['callsign'],
    "reg" => $_GET['reg'],
    "origin" => $_GET['origin'],
    "dest" => $_GET['dest'],
    "equipment" => $_GET['equipment'],
    "fuel" => $_GET['fuel'],
    "duration" => $_GET['duration'],
    "distance" => $_GET['distance'],
    "rep_url" => "Dummy",
    "more" => $_GET['more'],
    "fsacars_log" => $_GET['log']   // Get complete FSAcars log
);

/* DEBUG CODE - Write request to log file
*/
$fe = fopen (ERROR_LOG_PATH, "a");
fwrite($fe, "[DEBUG ".date("d.m.y H:i:s")."] PILOT: ".$_GET['pilot']." DATE: ".$_GET['date']." TIME: ".$_GET['time']." CALLSIGN: ".$_GET['callsign']." REG: ".$_GET['reg']." ORIG: ".$_GET['origin']." DEST: ".$_GET['dest']." EQUIP: ".$_GET['equipment']." FUEL: ".$_GET['fuel']." DURATION: ".$_GET['duration']." DIST: ".$_GET['distance']." MORE: ".$_GET['more']." LOG: ".$_GET['log']."\n");
fclose($fe);

return $fsacars_pirep;
}

function SavePIREPFile($pirep_array) {
/* ************************************************************************************************
       @SavePIREPFile
       Receives a string array with FSAcars pireps and creates or appends information to pirep file

   Inputs: string array
   Outputs: 1 sucess, 0 error
   ************************************************************************************************ */

/* Build report filename and URL */
$filename=$pirep_array['pilot'].str_replace("/","",$pirep_array['date']).str_replace(":","",$pirep_array['time']).".txt";
$pirep_array['rep_url']=REPORT_FILE_URL.$pirep_array['pilot']."/".$filename;

/* Parse FsAcars log */
$fsacars_log_lines_array = explode("*",$pirep_array['fsacars_log']);

/* Create or Append FSAcars report file */
$fp = fopen (REPORT_FILE_PATH.$pirep_array['pilot']."/".$filename, "a");

if (!$fp) {
    /* Error opening file */
    $fe = fopen (ERROR_LOG_PATH, "a");
    fwrite($fe, "[ERROR ".date("d.m.y H:i:s")."] PILOT: ".$pirep_array['pilot']." - ".ERROR_OPENING_REPORT_FILE." - ".$filename."\n");
    fclose($fe);

    return 0;
}

/*
* Write all log lines received from FSAcars */
for($i=0;$i<count($fsacars_log_lines_array);$i++) {
        if (!fwrite($fp, $fsacars_log_lines_array[$i] . "\n")) {
            /* Error writing to file */
            $fe = fopen (ERROR_LOG_PATH, "a");
        fwrite($fe, "[ERROR ".date("d.m.y H:i:s")."] PILOT".$pirep_array['pilot']." - ".ERROR_WRITING_REPORT_FILE." - ".$filename."\n");
        fclose($fe);

            return 0;
        }
}    

/* Close file */
fclose($fp);

return 1;
}

function InsertReportIntoDB($pirep_array) {
/* ************************************************************************************************
   @InsertReportIntoDB
   Receives a string array with FSAcars pireps and inserts summary into reports table

   Inputs: string array
   Outputs: 1 sucess, 0 error
   ************************************************************************************************ */

/* If this is the first chunk insert PIREP on database */
if ($pirep_array['more']=="0") {
        /* connect to database */
        include(MYSQL_CONNECT_INCLUDE);

        /*
         * Verify pilot identity (From VA Pilots table) */
        $the_pilot = $pirep_array['pilot'];

        $stmt = "select pilot_id from fsacars_pilots where pilot_num='$the_pilot'";
        $result = mysql_query($stmt);

        /* mysql error */
        if (!$result) {
            $fe = fopen (ERROR_LOG_PATH, "a");
        fwrite($fe, "[ERROR ".date("d.m.y H:i:s")."] ".ERROR_IN_PILOT_QUERY." - Pilot ".$pirep_array['pilot']." - ".mysql_error()." SQL: ".$stmt."\n");
        fclose($fe);

        return 0;
        }

        if (mysql_num_rows($result) == 0) {
            /* Pilot not found */
            $fe = fopen (ERROR_LOG_PATH, "a");
        fwrite($fe, "[ERROR ".date("d.m.y H:i:s")."] ".PILOT_NOT_FOUND." - Pilot ".$pirep_array['pilot']."\n");
        fclose($fe);

        return 0;
        } else {
                /* Pilot found */
                $pilot_id = mysql_result($result,0,"pilot_id");

                /* Insert info on reports table */
                $values = $pilot_id.",'".$pirep_array['date']."','".$pirep_array['time']."','".$pirep_array['callsign']."','".$pirep_array['origin']."','".$pirep_array['dest']."','".$pirep_array['reg']."','".$pirep_array['equipment']."','".$pirep_array['duration']."',".$pirep_array['fuel'].",".$pirep_array['distance'].",'".$pirep_array['rep_url']."'";
                $stmt = "INSERT INTO reports (pilot_id,date,time,callsign,origin_id,destination_id,registration,equipment,duration,fuel,distance,fsacars_rep_url) VALUES ($values)";                    
                $result = mysql_query($stmt);

                if (!$result) {
                    $fe = fopen (ERROR_LOG_PATH, "a");
                    fwrite($fe, "[ERROR ".date("d.m.y H:i:s")."] ".ERROR_INSERTING_PIREP." - Pilot ".$pirep_array['pilot']." - ".mysql_error()." SQL: ".$stmt."\n");
                    fclose($fe);

            return 0;
            }

    /* Close the database connection */
                mysql_close();
        }
    }

    return 1;
}


function main() {
/* ************************************************************************************************
   @main

   Inputs: N/A
   Outputs: "OK" sucess, "NOTOK" error
   ************************************************************************************************ */
$res = CheckFSAcarsInfo();
if ($res == 0) {
    return "NOTOK";
}

$a = GetFSAcarsInfo();

$res = SavePIREPFile($a);
if ($res == 0) {
    return "NOTOK";
}

$res = InsertReportIntoDB($a);
if ($res == 0) {
    return "NOTOK";
}


// Report sucessfully received
return "OK";
}

/* receive_pirep.php return to FSACARS */
$out = main();
echo $out;

?>

当我添加这个时,问题就来了;

function insertdcapirep($pirep_array) {
//  Read PIREP data from previously written file
       $filename=$pirep_array['pilot'].str_replace("/","",$pirep_array['date']).str_replace(":","",$pirep_array['time']).".txt";
$pirep_array['rep_url']=REPORT_FILE_URL.$pirep_array['pilot']."/".$filename;
$log=file(REPORT_FILE_PATH.$pirep_array['pilot']."/".$filename);

    //Find Pilot Number
    $dca = $pirep_array['pilot'];

    // Open database and pull pilot's name
    include(MYSQL_CONNECT_INCLUDE);
    $stmt="SELECT name FROM fsacars_pilots WHERE pilot_num='$dca'";
    $result=mysql_query($stmt);
    if (!$result) {
        $fe = fopen (ERROR_LOG_PATH, "a");
        fwrite($fe, "[ERROR ".date("d.m.y H:i:s")."] ".ERROR_IN_PILOT_QUERY." - Pilot ".$pirep_array['pilot']." - ".mysql_error()." SQL: ".$stmt."\n");
        fclose($fe);
        return 0;
        }
        if (mysql_num_rows($result) == 0) {
            $fe = fopen (ERROR_LOG_PATH, "a");
        fwrite($fe, "[ERROR ".date("d.m.y H:i:s")."] ".PILOT_NOT_FOUND." - Pilot ".$pirep_array['pilot']."\n");
        fclose($fe);
        return 0;
        } else {
        $name = mysql_result($result,0,"name");
        }
    //Find flight number
    $fltraw=explode(":",$log[1]);
    $flt=$fltraw[1];

    //find duration and convert to minutes
    $num=count($log);
    for($i=0;$i<$num;$i++) {
        if(stripos($log[$i],"Flight Duration")!== FALSE) {
            $duration=$log[$i];
            $total=substr($duration,-6);
            $rawtime=explode(":",$total);
            $hours=$rawtime[0]*60;
            $minutes=$hours+$rawtime[1];
    } }
    //Find Comments and save them to variable
    $num=count($log);
    for($i=0;$i<=$num;$i++) {
        if(stripos($log[$i],"comments")!== FALSE) {
        $rawcomments=strstr($log[$i],"comments");
        $almost_comments=ltrim($rawcomments,"comments");
        $comments=addslashes($almost_comments);
    } }
    // Calculate and assign date
        $timestamp=strtotime("now");    

    //Open and append data to database
    $stmt="INSERT INTO PIREPS VALUES ('',$dca,$name,$flt,$minutes,$comments,$timestamp)";
    $result=mysql_result($stmt);
    if (!$result) {
        $fe = fopen (ERROR_LOG_PATH, "a");
        fwrite($fe, "[ERROR ".date("d.m.y H:i:s")."] ".ERROR_INSERTING_PIREP." - Pilot ".$pirep_array['pilot']." - ".mysql_error()." SQL: ".$query."\n");
        fclose($fe);
        return 0;
        }
    mysql_close();
return 1;
}

...并在main函数下添加一个函数调用,像这样;

$res = insertdcapirep($a);
if(res==0) {
    return "NOTOK";
}

我知道这是很多代码,我深表歉意,如果有更好的地方让像我这样的初学者获得帮助,我很想去那里试试。我已经仔细研究了一个月,似乎有如此简单的东西,但我只是没有看到它。任何帮助将不胜感激!

最佳答案

我找到了解决方案。事实证明,每次收到数据包时,整个脚本都会循环,第一个函数实际上会检查是否存在任何内容。如果没有,脚本将停止。我能够使用

运行我自己的测试
if(strpos($pirep[$i],"TOD Land)!==FALSE) {
include("processdcapirep.php; }

检查日志文件的最后一行。这样,在写入整个文件之前它不会调用我的查询。我很感激我得到的每一个回应。看来这里有很多人!

关于php - 可执行文件有更多数据要发送,但不知道如何向可执行文件发送信号,表明已发送的数据已处理并准备好下一个 block ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30113682/

相关文章:

php - 使用php文件上传错误

javascript - 如何在 jquery 中获取 window.load 上的发布数据

MySQL 错误 2002 - 无法通过 64 位套接字 '/tmp/mysql.sock' 进行连接,32 位卸载失败使事情变得复杂

php - OnClick 函数获取下一条记录 PHP AJAX MYSQL

javascript - 简单函数中的"Uncaught SyntaxError: Unexpected token ( "

function - 泛型函数,类和 “where”关键字

javascript - laravel ajax 表单不会执行提交按钮

php - Elasticsearch距离查询异常抛出

php - 在php中处理带有键和值的foreach

javascript - 为什么javaScript中的这个循环函数不是每次都返回0?我将如何在 python 中做类似的事情?