制作PHP脚本自动写入文本文件

时间:2015-01-23 15:38:52

标签: php

我正在编写一个脚本,试图让我的日常工作更轻松。它可以部分工作,向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();

?>

1 个答案:

答案 0 :(得分:0)

我会使用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