简体   繁体   English

通过带有状态更新的 web 链接运行 PHP 批处理脚本

[英]Running PHP batch scripts through web link with status updates

I have a process defined in a batch file that runs 3 php scripts, one after another in sequence, and I want to create a web front-end for the process.我在批处理文件中定义了一个进程,该文件依次运行 3 个 php 脚本,我想为该进程创建一个 web 前端。 The process is triggered when someone uploads a file using a webform.当有人使用网络表单上传文件时,就会触发该过程。

I want the ability to notify the user after each script in the batch file is run with some useful messages, but I am not quite sure what the right way to go about is.我希望能够在批处理文件中的每个脚本运行后通知用户一些有用的消息,但我不太确定 go 的正确方法是什么。

I am thinking in terms of javascript that sends request to the 3 php files in sequence, with the php scripts echoing the status messages as they are executed.我正在考虑 javascript,它按顺序向 3 个 php 文件发送请求,其中 php 脚本在执行时回显状态消息。 But I would rather have the three files executed in a single trigger from the client instead of having javascript calling the the three scripts separately.但我宁愿让这三个文件在客户端的单个触发器中执行,而不是让 javascript 分别调用这三个脚本。

Any ideas whats the best way to go about it?关于 go 的任何想法是什么?

Store messages in the database or other centralized storage system.将消息存储在数据库或其他集中式存储系统中。 Use an ajax call to pull the newest messages.使用 ajax 调用来提取最新消息。

You could create a single php file that runs the 3 php script and echoes their ouputs.您可以创建一个运行 3 php 脚本并回显其输出的 php 文件。 By executing a server side request trough AJAX (I suggest jQuery framework ) the outputs may be collected and the client side scripts can process and show the results to the user.通过通过 AJAX(我建议jQuery 框架)执行服务器端请求,可以收集输出,客户端脚本可以处理并向用户显示结果。

Send an ajax call to a php file on your server.向服务器上的 php 文件发送 ajax 调用。 You don't specify how your php scripts are running, but what I would do is include other the other php files (which would have functions to process whatever they're supposed to do) and then call the functions from the main file.您没有指定 php 脚本是如何运行的,但我要做的是包含其他其他 php 文件(这些文件将具有处理它们应该执行的任何操作的函数),然后从主文件中调用这些函数。 These functions should return some sort of success/error messages that you can collect in an array.这些函数应该返回某种可以在数组中收集的成功/错误消息。 So something like this:所以是这样的:

$response[] = firstFileProcessing(); //your function names should be better
$response[] = secondFileProcessing();
$response[] = thirdFileProcessing();

When all the scripts are done processing, do:当所有脚本完成处理后,执行:

echo json_encode(array("responses" => $response));

Then back in your success function for the ajax call, you'd do:然后回到你的成功 function 调用 ajax ,你会做:

var res = jQuery.parseJSON(response_from_server); 
//or if you told ajax to expect a json response, 
//response_from_server would automatically be an object based on the json sent

and step through each one to see what php said.并逐一查看 php 所说的内容。

alert(res.responses[0]); //the first file said this

Or you could make your $response from php much more detailed - an array of its own, with $response['success'] = TRUE, $response['msg'] = "Worked,".或者,您可以使来自 php 的 $response 更加详细 - 它自己的数组,$response['success'] = TRUE, $response['msg'] = "Worked,"。 etc - any amount of data.等 - 任何数量的数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM