[PHP] 啟用php_snmp後出現「Cannot find module」

最近因為專案需要使用snmp和網路硬體設備做通訊,所以使用了php_snmp這個模組

修改完php.ini重新啟動服務後出現了許多錯誤訊息:

MIB search path: c:/usr/share/snmp/mibs
Cannot find module (IP-MIB): At line 0 in (none)
Cannot find module (IF-MIB): At line 0 in (none)
Cannot find module (TCP-MIB): At line 0 in (none)
Cannot find module (UDP-MIB): At line 0 in (none)
Cannot find module (HOST-RESOURCES-MIB): At line 0 in (none)
Cannot find module (SNMPv2-MIB): At line 0 in (none)
Cannot find module (SNMPv2-SMI): At line 0 in (none)
Cannot find module (NOTIFICATION-LOG-MIB): At line 0 in (none)
Cannot find module (UCD-SNMP-MIB): At line 0 in (none)
Cannot find module (UCD-DEMO-MIB): At line 0 in (none)
Cannot find module (SNMP-TARGET-MIB): At line 0 in (none)
Cannot find module (NET-SNMP-AGENT-MIB): At line 0 in (none)
Cannot find module (DISMAN-EVENT-MIB): At line 0 in (none)
Cannot find module (SNMP-VIEW-BASED-ACM-MIB): At line 0 in (none)
Cannot find module (SNMP-COMMUNITY-MIB): At line 0 in (none)
Cannot find module (SNMP-FRAMEWORK-MIB): At line 0 in (none)
Cannot find module (SNMP-MPD-MIB): At line 0 in (none)
Cannot find module (SNMP-USER-BASED-SM-MIB): At line 0 in (none)
Cannot find module (SNMP-NOTIFICATION-MIB): At line 0 in (none)
Cannot find module (SNMPv2-TM): At line 0 in (none)

 

原因是因為沒有安裝 SNMP MIB,解法當然是安裝 SNMP MIB,個人是找了 Net-SNMP  這套安裝,安裝完後基本上就可以直接使用了。

如果安裝完後還是不能用的話就自己手動加一個環境變數:

Variable Name:mibdirs
Variable Value:{Path to Net-SNMP install folder}\share\snmp\mibs (Default Install Path:C:\usr\share\snmp\mibs)
Leave a comment

[PHP] 使用str_pad()做字串填充

為了使用讓輸出看起來整齊一點,需要讓字串自己補位
這時候可以使用str_pad()來完成這件事情

string str_pad ( string $input , int $pad_length [, string $pad_string = " " [, int $pad_type = STR_PAD_RIGHT ]] );

說明一下參數:
$input:目標字串 (required)
$pad_length:需要補齊的目標長度 (required)
$pad_string:補齊的字元 (optional, Default:space)
$pad_type:補齊的類型,共支援STR_PAD_LEFT(補在左邊)、STR_PAD_RIGHT(補在右邊)、STR_PAD_BOTH(兩邊都補) (optional, Default:STR_PAD_RIGHT)

使用範例:

$input = "Calos";
echo str_pad($input, 10);                      // produces "Calos     "
echo str_pad($input, 10, "-=", STR_PAD_LEFT);  // produces "-=-=-Calos"
echo str_pad($input, 10, "_", STR_PAD_BOTH);   // produces "__Calos___"
echo str_pad($input, 6 , "___");               // produces "Calos_"

Reference:
http://php.net/manual/en/function.str-pad.php

Leave a comment

[PHP] 判斷json格式資料

直接利用json_decode

<?php
$str_1 = '{"number":"21","data":"abc"';
$str_2 = '<status>inherit</status><parent>113</parent>';
// output: true
echo ( json_decode($str_1) ? 'true' : 'false' );
// output: false
echo ( json_decode($str_2) ? 'true' : 'false' );
?>
Leave a comment