dsepcxw181184853 2014-09-29 14:44
浏览 59
已采纳

PHP在活动页面上显示后端脚本的输出

I have a web application in PHP. One component of the application is submitting the data to a backend pipeline (also written in PHP). The pipeline is an external script, which my application calls using 'exec' php function. The pipeline is a multistep pipeline which executes several programs taking input from the program run before. I want to be able to display a message on the application page which submits to the pipeline, with completion of each step in the pipeline, such as

Step 1 completed... Now doing step 2. Step 2 completed... Now doing step 3.

and so on and so forth.

I am open to use javascript/AJAX to do this, and also any other language compatible with PHP.

Any help is greatly appreciated.

  • 写回答

1条回答 默认 最新

  • duanguangsong2380 2014-09-29 14:50
    关注

    Well working on the assumption that you have some kind of database backing your PHP front-end and pipeline, you don't specifically need something compatible with PHP, but rather something that could interface with your database.

    Without having any further details on what you've set up/tried, etc. I can only offer an overview of the workflow I would use in this situation.

    • Front-end script is submitted and pushes the request into a processing queue.
    • User is shown a "processing, please wait" type page. This page makes a long-polling AJAX request or a Websocket connection to the front-end site to a script which polls the database for updates on the pipeline processing.
    • The pipeline scripts chain off each other and push into the database the details of their completion which are read off by the Websocket/long-polling front-end script and returned to the user via Javascript to display on the page.

    Using the database as a go-between would be the easiest and most flexible approach. You could also use other languages if you're more comfortable with them so long as they're compatible with your database used on the PHP/pipeline side.

    EDIT

    While I don't have any links to a tutorial on exactly what you want to do, there are some basics behind it that you can use to piece together your solution:

    I would start by getting your pipeline (processing) script to run in the background at interval using cron. Alternatively you could daemonize that pipeline using something like this PHP-Daemon framework so that it runs in the background. Perhaps having a cron task to check if it's running and restart it if needed.

    From there you can build a table in your database that contains status updates on the processing tasks at hand and build a PHP script that checks the status of a given task and outputs JSON data about it. This JSON data could easily be read using AJAX requests, the simplest of which would probably be jQuery's .ajax() method. This could be called at interval on the client side "status" page using a setTimeout call at the end of each "loop" to poll for status changes every X seconds. This would be the easiest implementation of what you're after, although not the best performing or optimal way to do it.

    So the general workflow here would change to this:

    • Front-end script is submitted and pushes the request into processing queue with status set to pending.
    • User is shown the processing page which pings status.php?task=12345 every X seconds.
    • Back-end daemon/cron script picks up the request and begins processing, pushing status updates into the database at interval.
    • status.php script begins to return different status information in the JSON code and it is displayed to the user.
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 准备学习小程序搭建,谁能手把手的教我啊?
  • ¥15 关于#嵌入式硬件#的问题:树莓派第一天重装配置python和opencv后第二天打开就成这样,瞎捣鼓搞出来文件夹还是没把原来的界面调回来
  • ¥20 Arduino 循迹小车程序电路出错故障求解
  • ¥20 Arduino 循迹小车程序电路出错故障求解
  • ¥100 AT89C52单片机C语言调试之后再回答
  • ¥15 AT89C52单片机C语言串口助手发送数据包返回值
  • ¥15 C++数组中找第二小的数字程序纠错
  • ¥50 MATLAB APP 制作出现问题
  • ¥15 wannier复现图像时berry曲率极值点与高对称点严重偏移
  • ¥15 利用决策森林为什么会出现这样·的问题(关键词-情感分析)