[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

[ZendFramework] ZendFramework 2 安裝

取得ZF2的libs有兩個方式,一是使用composer,二是從Zend的github clone

 

利用composer安裝
※注意,若以下任何指令失敗並出現「Permission Denied」,請配合sudo操作。

# 先用curl取得composer.phar
curl -sS https://getcomposer.org/installer | php

# 用composer建立專案 (專案名稱以zf2-test為例)
php composer.phar create-project --repository-url="https://packages.zendframework.com" -s dev zendframework/skeleton-application ./zf2-test
cd zf-test/
php composer.phar update

 

若執行 php composer.phar update 出現 「Warning: This development build of composer is over 30 days old. It is recommended to update it by running “composer.phar self-update” to get the latest version.」,則改用self-update

php composer.phar self-update

 

若出現執行逾時的狀況,可加長執行時間

# 出現執行逾時會出現以下訊息
[RuntimeException]
The process timed out.

# 設定執行時間上限為3600秒
COMPOSER_PROCESS_TIMEOUT=3600 php composer.phar install
COMPOSER_PROCESS_TIMEOUT=3600 php composer.phar update

 

從GitHub安裝
※Windows平台建議使用此方法安裝(雖然composer官方有Windows的Installer,but…直接從GitHub拉回來比較省事啦XD)
※Windows使用者請記得將php.exe的路徑加入系統環境變數內,否則你的cmd會不認識php
※Windows使用者若沒有bash環境可用,請自行將第二行的mv視為rename

git clone https://github.com/zendframework/ZendSkeletonApplication.git
mv ZendSkeletonApplication zf2-test
cd zf2-test
php composer.phar self-update
php composer.phar install
php composer.phar update

 

設定Apache
在apache設定檔中加入以下內容,將DNS指到zf2-test/public

<VirtualHost *:80>
    ServerName zf-test.localhost
    DocumentRoot /path/to/zf2-test/public

    <Directory /path/to/zf-test/public>
        DirectoryIndex index.php
        AllowOverride All
        <IfVersion < 2.3 >
            Order allow,deny
            Allow from all
        </IfVersion>

        <IfVersion >= 2.3 >
            Require all granted
        </IfVersion>
    </Directory>
</VirtualHost>

 

打開瀏覽器輸入zf-test.localhost,即可看到default page

若沒有DNS的話,也可以開一個port指過去zf2-test/public

NameVirtualHost *:8080
Listen 8080
<VirtualHost *:8080>
    DocumentRoot /path/to/zf2-test/public

    <Directory /path/to/zf2-test/public>
        DirectoryIndex index.php
        AllowOverride All
        <IfVersion < 2.3 >
            Order allow,deny
            Allow from all
        </IfVersion>

        <IfVersion >= 2.3 >
            Require all granted
        </IfVersion>
    </Directory>
</VirtualHost>

 

打開瀏覽器輸入localhost:8080,即可看到default page

Leave a comment