Use PHP installation services windows that run automatically, PHP program can be achieved for a long time, run automatically, regularly updated features, it can be used directly in the project class source code ...


 

What  windows service 

           Windows service (ie, the previous NT service) allows you to create executable application in their own Windows sessions can be long-running . These services can be started automatically when the computer starts, you can pause and restart and does not display any user interface. This makes the service ideal for use on the server, or at any time, in order not to affect other users working on the same computer, in case you need to run the function for a long time. Also can be different from the specific user account logged-on user or the default security context of the computer account to run the service. "" ( Baidu Encyclopedia ) 

  • Simply means that you can for a long time, automatically run  the PHP on Windows. Q: This is important for me to do?

 

 

What is the use?

       Ha ha ha! With this, we can do more things. Such as: PHP regularly perform a task, the data is automatically updated .... and so on.

 

How to achieve?

    premise:

  1. There must be a Windows server or Windows PC machines
  2. Must be installed must have PHP runtime environment
  3. This must be in the \ ext \ directory under the PHP installation php_win32service.dll file
  4. There must be turned inside php.ini file  extension = php_win32service.dll   this option

 

If you are not up and running (see description above a ha oh) to the most important codes, as follows

<?
/**
 * 利用PHP安装windows自动运行的服务
 *
 * Project: Tiwer Developer Framwork
 * This is NOT a freeware, use is subject to license terms!
 * 
 * Site: http://wgw8299.cnblogs.com
 * 
 * $Id: WinService.class.php 258 2011-03-07 02:18:42Z wgw8299 $
 *
 * Copyright (C) 2007-2010 Tiwer Developer Team. All Rights Reserved.
 */
 class WinService
 {
	/**
	 * 服务名称
	 */
	var name;
	
	/**
	 * 定义服务名称
	 */
	var info_name;
	
	/**
	 * 定义php.exe存放路径
	 */
	var path;
	
	/**
	 * 定义所要执行的程序
	 */
	var params;
	
	/**
	 * 定义程序分隔执行时间,单位:秒
	 */
	var sleep = 5;
	
	/**
	 * 构造函数
	 *
	 * @access private
	 * 
	 * @return void
	 */
	private function __construct() {
	
	}
	
	/**
	 * 安装服务
	 *
	 * @access public
         *
	 * @return void
	 */
	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 ( '服务创建成功!' ) ; 
		} 
	}
	
	/**
	 * 卸载服务
	 *
	 * @access public
         *
	 * @return void
	 */
	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 ; 
		}
	}
	
	/**
	 * 重启服务
	 *
	 * @access public
         *
	 * @return void
	 */
	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   "服务已启动! "; 
					 } 	
				}    
			} 
		} 
	}
	
	/**
	 * 启动服务
	 *
	 * @access public
     *
	 * @return void	 
	 */
	public function start() {	
		$s = win32_start_service(_SERVICENAME); 		  
		if ( $s != 0 ){ 
			echo   " 服务正在运行中! " ; 
		  } else { 
			echo   " 服务已启动! " ; 
		  }
	}
	
	/**
	 * 停止服务
	 *
	 * @access public
     *
	 * @return void
	 */
	public function stop() {	
		$s = win32_stop_service(_SERVICENAME ); 		  
		if ( $s != 0 ){ 
			echo   " 服务未启动! " ; 
		} else { 
			echo   " 服务已停止! " ; 
		} 
	}
	
 } 
?>

  


      

Reproduced in: https: //my.oschina.net/tiwer/blog/199857

Guess you like

Origin blog.csdn.net/weixin_34399060/article/details/91708533