0

我使用 xampp php apache 服务器

我不能

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

我想

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

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

4

2 回答 2

1

这……有点复杂。

我想

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 秒运行一次脚本,要么在脚本终止后立即重新启动它。

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

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

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

示例工作流程:

- 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 客户端在写入“Work has been queued, please wait”后终止,并发送一个 HTTP 头循环回自身:

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

假设二 - 15:56:19 客户等待,比如说,十秒钟,以防万一,每秒运行一次检查,看看工作是否完成。如果是,则按照 15:17:35 进行;else 与假设一一样,说“我会回来”,并以 Location 标头而死。- 15:56:20 客户端脚本,多亏了 Location 标头,重新加载了 check=12345。它与数据库连接,看到 12345 的状态未完成,显示“正在工作...”动画,等待五秒钟,然后再次死亡,并显示 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 上的套接字,客户端脚本在需要做某事时“唤醒”它。这减少了请求和回答之间的延迟。

于 2012-07-15T14:11:53.670 回答
0

将 php 作为服务运行,php 可以在没有连接到客户端的情况下运行

您可以使用 cron 任务运行 PHP 脚本

于 2012-07-15T12:17:36.700 回答