助力软件开发企业降本增效 PHP / java源码系统,只需一次付费,代码终身使用! 广告
win32service扩展是Windows特定的扩展,允许PHP与服务控制管理器进行通信以启动,停止,注册和注销服务,甚至允许您的PHP脚本作为服务运行 [https://pecl.php.net/package/win32service](https://pecl.php.net/package/win32service) * [预定义常量](https://www.php.net/manual/en/win32service.constants.php) * [Win32Service服务类型位掩码](https://www.php.net/manual/en/win32service.constants.servicetype.php) * [Win32Service服务状态常数](https://www.php.net/manual/en/win32service.constants.servicestatus.php) * [Win32Service服务控制消息常量](https://www.php.net/manual/en/win32service.constants.servicecontrol.php) * [Win32Service服务控制消息接受的位掩码](https://www.php.net/manual/en/win32service.constants.controlsaccepted.php) * [Win32Service服务启动类型常量](https://www.php.net/manual/en/win32service.constants.servicestarttype.php) * [Win32Service服务错误控制常数](https://www.php.net/manual/en/win32service.constants.errorcontrol.php) * [Win32Service服务标志常量](https://www.php.net/manual/en/win32service.constants.serviceflag.php) * [Win32错误代码](https://www.php.net/manual/en/win32service.constants.errors.php) * [Win32基本优先级类](https://www.php.net/manual/en/win32service.constants.basepriorities.php) * [Win32恢复操作](https://www.php.net/manual/en/win32service.constants.recovery-action.php) * [Win32服务信息](https://www.php.net/manual/en/win32service.constants.serviceinfos.php) * [例子](https://www.php.net/manual/en/win32service.examples.php) * [win32service函数](https://www.php.net/manual/en/ref.win32service.php) * [win32\_continue\_service](https://www.php.net/manual/en/function.win32-continue-service.php)—恢复暂停的服务 * [win32\_create\_service](https://www.php.net/manual/en/function.win32-create-service.php)—在SCM数据库中创建新的服务条目 * [win32\_delete\_service](https://www.php.net/manual/en/function.win32-delete-service.php)—从SCM数据库中删除服务条目 * [win32\_get\_last\_control\_message](https://www.php.net/manual/en/function.win32-get-last-control-message.php)—返回发送到该服务的最后一条控制消息 * [win32\_pause\_service](https://www.php.net/manual/en/function.win32-pause-service.php)—暂停服务 * [win32\_query\_service\_status](https://www.php.net/manual/en/function.win32-query-service-status.php)—查询服务状态 * [win32\_send\_custom\_control](https://www.php.net/manual/en/function.win32-send-custom-control.php)—将自定义控件发送到服务 * [win32\_set\_service\_exit\_code](https://www.php.net/manual/en/function.win32-set-service-exit-code.php)—定义或返回当前正在运行的服务的退出代码 * [win32\_set\_service\_exit\_mode](https://www.php.net/manual/en/function.win32-set-service-exit-mode.php)—定义或返回当前正在运行的服务的退出模式 * [win32\_set\_service\_status](https://www.php.net/manual/en/function.win32-set-service-status.php)—更新服务状态 * [win32\_start\_service\_ctrl\_dispatcher](https://www.php.net/manual/en/function.win32-start-service-ctrl-dispatcher.php)—向SCM注册脚本,以便它可以使用给定名称充当服务 * [win32\_start\_service](https://www.php.net/manual/en/function.win32-start-service.php)—启动服务 * [win32\_stop\_service](https://www.php.net/manual/en/function.win32-stop-service.php)—停止服务 Windows 服务(即,以前的 NT 服务)使您能够创建在它们自己的 Windows 会话中可长时间运行的可执行应用程序 。 这些服务可以在计算机启动时自动启动,可以暂停和重新启动而且不显示任何用户界面。这使服务非常适合在服务器上使用,或任何时候,为了不影响在同一台计算 机上工作的其他用户,需要长时间运行功能时使用。还可以在不同于登录用户的特定用户帐户或默认计算机帐户的安全上下文中运行服务 简单的说就是可以长时间、自动运行在Windows上的PHP程序。 问:这对我来说重要吗? 有什么用?  哈哈哈!有了这个,我们就可以做更多的事了。如:定期执行一个PHP任务、自动更新数据 ....等等。 如何实现? 必须有一台 Windows服务器 或者Windows PC机 必须安装得有PHP运行环境 必须在PHP 的安装目录的\\ext\\下有这个 php\_win32service.dll文件 必须有php.ini文件里面开启  extension=php\_win32service.dll 这个选项 如果你运行不起来(请看一哈上面的说明哦)最重要代码来了,如下 ``` <?php /** * 利用PHP安装windows自动运行的服务 * $Id: WinService.class.php * $winService = new WinService(); * $winService->install(); */ class WinService { //服务名称 var $name = 'PHP Service'; //定义服务名称 var $info_name = "Exsample PHP Service"; //定义php.exe存放路径 var $path = "C:\\wamp\\php\\php.exe"; //定义所要执行的程序 var $params = "D:\\localhost\\Service\\win32_service.php"; //定义程序分隔执行时间,单位:秒 var $sleep = 5; private function __construct($name = '', $infoName = '', $param = '') { $this->name = $name; $this->info_name = $infoName; $this->params = $param; } public function install() { /* 注册服务 */ $x = win32_create_service(array( 'service' => $this->name, 'display' => $this->info_name, 'path' => $this->path, 'params' => $this->params, )); /* 启动服务 */ win32_start_service($this->name); if ($x !== true) { die ('服务创建失败!'); } else { die ('服务创建成功!'); } } public function uninstall() { /* 移除服务 */ $removeService = win32_delete_service($this->name); switch ($removeService) { case 1060: die ('服务不存在!'); break; case 1072: die ('服务不能被正常移除! '); break; case 0: die ('服务已被成功移除!'); break; default : die (); break; } } public function restart() { /* 重启服务 */ $svcStatus = win32_query_service_status($this->name); if ($svcStatus == 1060) { echo "服务[" . $this->name . "]未被安装,请先安装"; } else { if ($svcStatus['CurrentState'] == 1) { $s = win32_start_service($this->name); if ($s != 0) { echo "服务无法被启动,请重试! "; } else { echo "服务已启动! "; } } else { $s = win32_stop_service($this->name); if ($s != 0) { echo " 服务正在执行,请重试! "; } else { $s = win32_start_service($this->name); if ($s != 0) { echo "服务无法被启动,请重试! "; } else { echo "服务已启动! "; } } } } } public function start() { $s = win32_start_service($this->name); if ($s != 0) { echo " 服务正在运行中! "; } else { echo " 服务已启动! "; } } public function stop() { $s = win32_stop_service($this->name); if ($s != 0) { echo " 服务未启动! "; } else { echo " 服务已停止! "; } } } ?> ``` win32_service.php ``` <?php //检测服务是否存在 if (!win32_start_service_ctrl_dispatcher("PHP Service")) { die("没有发现正在运行的 [ "PHP Service" ] 服务"); } win32_set_service_status(WIN32_SERVICE_START_PENDING); win32_set_service_status(WIN32_SERVICE_RUNNING); //如果运行中 while (WIN32_SERVICE_CONTROL_STOP != win32_get_last_control_message()) { //写入文件 for ($i = 1; $i <= 1; $i++) { $b_file_path = "D:\\localhost\\test.txt"; $f = fopen($b_file_path, 'a+'); $msg = 'Dernier backup correctement:' . date('y/m/d h:i:s'); fwrite($f, $msg . "\r\n"); fclose($f); sleep(1); } } ?> ```