background real-time monitoring server's CUP and memory occupancy rate scenarios is very common. Although I haven't done it yet, I didn't think it would take more than 2 hours before I realized the handwritten code. Although the

online search PHP CUP memory usage of this kind of key words out of the article a lot, but most of them are in the Linux environment, windows proposed by the CMD statement only view, but there is little direct practical feasible code, try again after wmic, the systeminfo is not familiar with the CMD command, and did not find a feasible solution, a final answer by Baidu know to find ways, using VBS to obtain the slightly improved, can be used directly in the windows environment.


 class SystemInfoWindows {  /**   * 判断指定路径下指定文件是否存在,如不存在则创建   * @param string $fileName 文件名   * @param string $content 文件内容   * @return string 返回文件路径   */  private function getFilePath($fileName, $content)  {   $path = dirname(__FILE__) . "\$fileName";   if (!file_exists($path)) {    file_put_contents($path, $content);   }   return $path;  }  /**   * 获得cpu使用率vbs文件生成函数   * @return string 返回vbs文件路径   */  private function getCupUsageVbsPath()  {   return $this->getFilePath(    'cpu_usage.vbs',    "On Error Resume Next     Set objProc = GetObject("winmgmts:\\.\rootcimv2:win32_processor='cpu0'")     WS Cript.Echo (objProc.LoadPercentage) ");} / * * * to get the total memory and physical memory available to JSON VBS file generation function * @return string return VBS function getMemoryUsageVbsPath / private file path () {return $this-> getFilePath ('memory_usage.vbs'On Error Resume Next," Set objWMI = GetObject ("winmgmts:\\.\rootcimv2") Set objWMI.InstancesOf (colOS =  "Win32_OperatingSystem" For Each objOS in colOS Wscript.Echo) ("{  " TotalVisibleMemorySize  ": " & objOS.TotalVisibleMemorySize &  ", "  "FreePhysicalMemory ":  "& objOS.FreePhysicalMemory &" Next "}"));} / * * * CPU use rate * @return Number public * / function (getCpuUsage) {$path = $this-> getCupUsageVbsPa (th); exec ("cscript -nologo $path", $usage); return $usage[0];} / * * * * @return array obtained memory usage array / public function (getMemoryUsage) {$path = $this-> (getMemoryUsageVbsPath); exec ("cscript -nologo $path", $usage); $memory = json_decode ($usage[0], true) ((Round; $memory['usage'] = ($memory['TotalVisibleMemorySize'] - $memory['FreePhysicalMemory']) / $memory['TotalVisibleMemorySize']) * 100); return $memory;}}

 $info = new call mode (SystemInfoWindows); $cpu = $info-> (getCpuUsage); $memory = $info-> getMemoryUsage (echo); "the current system CPU usage: {$cpu}%, memory usage the rate of {$memory['usage']}% 

above is the PHP that is introduced to you by PHP. We get the real-time usage rate of CPU memory under the environment of windows. We hope to help you. If you have any questions, please leave a message for me, Xiaobian will reply to you in time. Thank you for your support for the home website of the script!

This paper fixed link: | Script Home | +Copy Link

Article reprint please specify:PHP’s real-time use rate of CPU memory in the windows environment (recommended) | Script Home

You may also be interested in these articles!