How to build php triggerable service

2019-06-12 04:04发布

问题:

I use xampp php apache server

I can't

  1. continue php after exit
  2. send response to client before exit
  3. synchronize http request with resources sharing

I want

  1. run php as service where php may run without being connected to client
  2. php execution can be triggered using http request by client
  3. php (continuous or triggered) have full access to the resources
  4. php can send the data from the resources to the client on response

Is there any way I can do these things OR should I try look into any other servers OR socket programming is the only way?

回答1:

This is... a bit complicated.

I want

run php as service where php may run without being connected to client
php execution can be triggered using http request by client
php (continuous or triggered) have full access to the resources
php can send the data from the resources to the client on response

The first thing is easily achievable. Simply install PHP as command line, and run it in an infinite loop (command line has no expiration time) or, maybe better for what you need, as crontab service. There are several utilities to achieve this; they either run a script every X seconds, or relaunching it as soon as the script terminates.

If the script somehow (e.g. through a database) checks whether it has "work to do", then your objective 2 is achieved also: the HTTP web client script has only to insert the details in the database, and it will be as if the master running script has been launched by the Web server. What actually happens is that the master script usually terminates immediately (maybe sleeping a few seconds, not to overload the server), but the next time it runs, it finds work to do and does it.

Your objective 3 is then achievable by running the master script with more privileges. The user script cannot "access all resources", but can humbly ask for data, and the master script (more secure) may graciously allow such data to be read -- and written to the database (or the filesystem) where the client may get it.

Finally, your objective 4 is achievable by reading the data and supplying them to the client browser on the client script's part.

A sample workflow:

- 15:55:00 The master script runs. SELECT worktodo FROM thingstodo returns nothing
- 15:56:00 The master script runs. SELECT worktodo FROM thingstodo returns nothing
- 15:56:17 The client runs; it is a request for data from serial port.
- 15:56:17 The request is stored in the DB and gets ID 12345 and status "WAITING"

Hypothesis one - 15:56:18 The client terminates after writing "Work has been queued, please wait" and sends a HTTP header looping back to itself:

    Location: http://mylocation/script.php?check=12345

Hypothesis two - 15:56:19 The client waits for, say, ten seconds, just in case, running a check every second, to see if the work got done. If it did, then proceed as per 15:17:35; else does as in hypothesis one, says "I'll be back" and dies with a Location header. - 15:56:20 The clients script, thanks to the Location header, gets reloaded with check=12345. It connects with the DB, sees that status of 12345 is not COMPLETED, displays a "Working..." animation, waits say five seconds, and dies with again a Location: .

- 15:57:00 The master script gets awakened and finds there is one job in WAITING status; updates it in LOCKED status and places its own IP and PID in the proper fields, then decodes the work details, sees what needs to be done and does it.
- 15:57:33 The work is done and the row gets updated again; now STATUS=COMPLETED
- 15:57:35 Meanwhile the client gets called again, but this time it finds COMPLETED, so happily fetches the data and shows it to the customer, also deleting the row not to clutter the DB.

Other possibilities: - the master script, finding an Email field filled in the job row, sends an email with an appropriate link to the user. - the master script actually sleeps most of the time, bound to a socket on port XYZ, and the client script "awakens" it when something needs to be done. This reduces delay between request and answer.



回答2:

run php as service where php may run without being connected to client

You can run PHP script using cron task