如何构建php可触发服务 [英] How to build php triggerable service

查看:17
本文介绍了如何构建php可触发服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 xampp php apache 服务器

I use xampp php apache server

我不能

  1. 退出后继续php
  2. 退出前向客户端发送响应
  3. 同步http请求与资源共享

我想要

  1. 将 php 作为服务运行,其中 php 可以在不连接到客户端的情况下运行
  2. 客户端可以使用http请求触发php执行
  3. php(连续或触发)拥有对资源的完全访问权限
  4. php 可以在响应时将数据从资源发送到客户端

有什么办法可以做这些事情,还是我应该尝试查看任何其他服务器,或者套接字编程是唯一的方法?

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?

推荐答案

这……有点复杂.

我想要

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

第一件事很容易实现.只需将 PHP 安装为命令行,并在无限循环中运行它(命令行没有过期时间),或者,作为 crontab 服务,可能更适合您的需要.有几个实用程序可以实现这一点;他们要么每 X 秒运行一个脚本,要么在脚本终止后立即重新启动它.

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.

如果脚本以某种方式(例如通过数据库)检查它是否有工作要做",那么您的目标 2 也实现了:HTTP Web 客户端脚本只需在数据库中插入详细信息,它将是好像主运行脚本已经被 Web 服务器启动了.实际发生的情况是,主脚本通常会立即终止(可能会休眠几秒钟,而不是为了使服务器过载),但下次运行时,它会找到要做的工作并执行.

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.

然后可以通过以更多权限运行主脚本来实现您的目标 3.用户脚本不能访问所有资源",但可以谦虚地请求数据,并且主脚本(更安全)可能会慷慨地允许读取此类数据——并写入客户端可能获得的数据库(或文件系统)

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.

最后,您的目标 4 可以通过读取数据并将其提供给客户端脚本部分的客户端浏览器来实现.

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

示例工作流程:

- 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"

假设一- 15:56:18 客户端在写入工作已排队,请稍候"后终止,并发送一个 HTTP 标头循环回自身:

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

假设二- 15:56:19 客户端等待,比如说,十秒钟,以防万一,每秒运行一次检查,看看工作是否完成.如果是,则按 15:17:35 继续;else 和假设一一样,说我会回来"并以 Location 标题结束.- 15:56:20 由于位置标头,客户端脚本重新加载了 check=12345.它与数据库连接,看到 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.

其他可能性:- 主脚本找到在作业行中填写的电子邮件字段,向用户发送带有适当链接的电子邮件.- 主脚本实际上大部分时间都处于休眠状态,绑定到端口 XYZ 上的套接字,并且客户端脚本在需要执行某些操作时唤醒"它.这减少了请求和应答之间的延迟.

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.

这篇关于如何构建php可触发服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆