使 PHP 脚本自动写入文本文件


Making PHP Script Automatically write to text file

我正在编写一个脚本,试图帮助我的日常工作更轻松。它分部分工作,向 API 发送请求并接收响应。然后,我希望它将所述响应保存在文本文件中,该文件将自动通过电子邮件发送到定义的邮箱。我坚持的一点实际上是将我的脚本保存到文本文件中。

这是我的脚本:(我不是很有经验......包含文件不相关,因为它们向 API 发送请求并且不包含任何其他内容。我希望我可以在SQL连接关闭后在底部添加一行,告诉它保存到文本文件中。另外,我希望文本文件采用"$CorrectCLI"变量的名称。

任何帮助,不胜感激。干杯。

// Check connection
    if(mysqli_connect_errno()) {
        echo "Could Not Connect, Contact Support.";
        exit;
    } else {
        echo "Connected to Database Successfully.<br><br>";
    }
//Required Information:
    $CorrectCLI = $CLIrow[0];
    $CorrectSitename = $SiteNamRow[0];
    $CorrectSiteNumber = $SiteNumRow[0];
    $CorrectIP = $IProw[0];;

$xrefFromKF = "503269";
echo nl2br("Affected XREF: " . $xrefFromKF . "'n");
$CLIquery = "select primary_cli from customers where xref = " . $xrefFromKF . "";
$CLIresult = mysqli_query($con,$CLIquery);
if(!$CLIresult) {
    echo "Could Not Locate CLI In Database.<br>";
}
$CLIrow = mysqli_fetch_row($CLIresult);
$CLI = $CLIrow[0];
echo nl2br("Circuit CLI: 0" . $CLIrow[0] . "<br>");
if(!$CLI > 0 ) {
    echo " Could Not Locate CLI.<br>";
    }
$IPquery =  "select manufacturers_ip_prim from customers where primary_cli = " . $CLIrow[0] . "";
$result = mysqli_query($con,$IPquery);
if(!$result) {
    echo "Could Not Locate Management IP.<br>";
}
$IProw = mysqli_fetch_row($result);
$IPcount = $IProw[0];
echo nl2br("Circuit IP: " . $IProw[0] . "");
if(!$IPcount > 0 ) {
    echo " Could Not Locate Management IP.<br>";
    }
        echo nl2br("'n'nPinged " . $IPcount . "...'n");
        exec("ping -c 2 " . $IPcount, $output1, $result1);
        if ($result1 == 0) {
            echo nl2br("Circuit Recovered - Cancelled Diagnostics.");
        } else {
            echo nl2br("Circuit Down - Starting Diagnostics");
            // Perform Sync Check
            include '../diags/SyncStatus.php';
            // Perform Data Port Reset
            include '../diags/DataPortReset.php';
                // sleep(10);
            echo nl2br("'nPinged " . $IPcount . "...'n");
            exec("ping -c 2 " . $IPcount, $output2, $result2);
            if ($result2 == 0) {
                echo nl2br("Circuit Recovered - Stopping Diagnostics.");
                } else {
                echo nl2br("Circuit Still Down - Continuing Diagnostics'n'n");
                include '../diags/Metascript.php';  
                include '../diags/TAMtestSubmit.php';
                include '../diags/TAMtestRetrieve.php'; 
                    echo "<br>";            
                include '../diags/GetProfile.php';
                    echo "<br>";
                include '../diags/ProfileOverride.php';
                    echo "<br>";
                // sleep(300);
                include '../diags/RevertProfile.php';
                    echo "<br>";
                echo nl2br("'nPinged " . $IPcount . "...'n");
                exec("ping -c 2 " . $IPcount, $output4, $result4);
                    //print_r($output4);
                if ($result4 == 0) {
                    echo nl2br("Circuit Recovered - Stopping End Of Diagnostics.");
                } else {
                    echo nl2br("Circuit Still Down - Requires Attention.'n'n");
                }
            }
        }
// Close the Database Connection
    $conn->close();
?>

我会使用 PHP 输出缓冲区。

将此代码添加到此 PHP 脚本的顶部

ob_start("process_output");
function process_output($buffer){
        $pageContent = $buffer;
        // $pageContent is all the output of your web page, 
        // put your email logic here
        return $pageContent;
}

在你的逻辑中,我会参考 http://php.net/manual/en/function.file-put-contents.php