为了在 Unix 中使用 SNMP 函数,需要安装 NET-SNMP 包。在 Windows 中,这些函数只能用于 NT,而不能用于 Win95/98。


重要提示:为了使用 UCD SNMP 包,需要在编译之前将 NO_ZEROLENGTH_COMMUNITY 定义为 1。 在配置 UCD SNMP 之后,编辑 config.hacconfig.h,查找 NO_ZEROLENGTH_COMMUNITY,将 #define 所在行的注释去掉。修改后应该类似这样:
然后使用 --with-snmp[=DIR] 选项编译 PHP。

如果在组合 SNMP 命令时看到奇怪的字段错误,那就是因为没有遵从上述说明。如果不想重新编译 UCD SNMP,可以使用 --enable-ucd-snmp-hack 开关编译 PHP 以绕开上述错误。

Windows 版本在目录 mibs 中包含了支持 SNMP 的文件。此目录应该移到 DRIVE:\usr\mibs,其中 DRIVE 是安装 PHP 所在的盘符,例如 c:\usr\mibs


本扩展模块在 php.ini 中未定义任何配置选项。


以下常量由本扩展模块定义,因此只有在本扩展模块被编译到 PHP 中,或者在运行时被动态加载后才有效。




SNMP_BIT_STR (integer)

SNMP_OCTET_STR (integer)

SNMP_OPAQUE (integer)

SNMP_NULL (integer)

SNMP_OBJECT_ID (integer)


SNMP_COUNTER (integer)




SNMP_INTEGER (integer)

SNMP_COUNTER64 (integer)

snmp_get_quick_print --  返回 UCD 库中 quick_print 设置的当前值
snmp_get_valueretrieval --  Return the method how the SNMP values will be returned
snmp_read_mib --  Reads and parses a MIB file into the active MIB tree
snmp_set_enum_print --  Return all values that are enums with their enum value instead of the raw integer
snmp_set_oid_numeric_print --  Return all objects including their respective object id within the specified one
snmp_set_quick_print --  设置 UCD SNMP 库中 quick_print 的值
snmp_set_valueretrieval --  Specify the method how the SNMP values will be returned
snmpget -- 获取一个 SNMP 对象
snmpgetnext --  Fetch a SNMP object
snmprealwalk --  返回指定的所有对象,包括它们各自的对象 ID
snmpset -- 设置一个 SNMP 对象
snmpwalk -- 从代理返回所有的 SNMP 对象
snmpwalkoid -- 查询关于网络实体的信息树

add a note add a note User Contributed Notes
1413 at blargh dot com
11-Nov-2005 05:29
For my purposes in PHP coding, I have the following SNMP options set, always.  These aren't documented very well in PHP's docs, but they make dealing with the SNMP results a LOT easier:

// Return back the numeric OIDs, instead of text strings.

// Get just the values.

// For sequence types, return just the numbers, not the string and numbers.

// Don't let the SNMP library get cute with value interpretation.  This makes
// MAC addresses return the 6 binary bytes, timeticks to return just the integer
// value, and some other things.
03-Nov-2005 01:42
I had a problem getting mib errors on my php5 and IIS install.  Ended up adding an environmental variable to the Win2k box and that fixed it.  Had to add MIBDIRS=C:\PHP\extras\mibs

After I added this and booted the server I was no longer getting the errors.
ch at westend dot com
20-Oct-2005 03:57
For some devices one needs a string index, i.e.

On commandline this is possible with the "-Ir" flag. PHP does not
seem to like it regardless of the quoting. Converting the string index to a numeric OID is quite simple though, it becomes:

function getNumericStringIndex($string_idx) {
     $string_idx = substr($string_idx, 1, strlen($string_idx)-2);
     $ret = strlen($string_idx);
     for ($i=0; $i<strlen($string_idx); $i++)
       $ret .= '.'.ord($string_idx[$i]);

     return $ret;
Jeroen Simonetti
13-Jun-2005 07:16
The listed functions here use snmp version 1. If you wish to use version 2c or 3, use the following functions:

snmp v2c functions:

snmp2_get (string host, string community, string object_id [, int timeout [, int retries]])
snmp2_getnext (string host, string community, string object_id [, int timeout [, int retries]])
snmp2_walk (string host, string community, string object_id [, int timeout [, int retries]])
snmp2_real_walk (string host, string community, string object_id [, int timeout [, int retries]])
snmp2_set (string host, string community, string object_id, string type, mixed value [, int timeout [, int retries]])

snmp v3 functions:

snmp3_get (string host, string sec_name, string sec_level, string auth_protocol, string auth_passphrase, string priv_pr)
snmp3_getnext (string host, string sec_name, string sec_level, string auth_protocol, string auth_passphrase, string pri)
snmp3_walk (string host, string sec_name, string sec_level, string auth_protocol, string auth_passphrase, string priv_p)
snmp3_real_walk (string host, string sec_name, string sec_level, string auth_protocol, string auth_passphrase, string p)
snmp3_set (string host, string sec_name, string sec_level, string auth_protocol, string auth_passphrase, string priv_pr)
30-Mar-2005 04:42
It seems obvious enough in retrospect, but it doesn't say it anywhere in the (rather poor) SNMP functions documentation.

You can use a non-standard port number in the hostname string.

tom at tom420 dot cjb dot net
02-Sep-2004 03:52
As of Net-SNMP 5.1.2 the file config.h is now called acconfig.h. This is where you will find and uncomment the line
as described in the manual above.

I've been looking for the file for about 20 minutes before I located it by the new name.
amit_gupta at users dot sourceforge dot net
16-Jul-2004 09:01
I was struggling to compile PHP 4.3.8/PHP-5 with net-snmp-5.1 RPMS .  It was continously showing errors during make. Later I first complied and installed net-snmp-5.1  on fedora system. With this compiled Net-snmp , I could install PHP 4.3.8/php5 successfully. So if you are also getting error during make command during installation of PHP, first compile net-snmp instaead of using its RPM.

Amit Gupta
peterd at nospam telephonetics co uk
16-Jun-2004 03:37
I've found that when using SNMP on Windows (2000 Pro) the drive the mib files have to be located on actually depends on where you subsequently call PHP from.

For example:
(Assume PHP is installed in c:\\php and the mibs are installed in c:\\usr\\mibs and you have a script c:\\test.php)
Open a command prompt at c:\\
Calling c:\\php\\cli\\php.exe c:\\test.php will work fine.
Change directory to d:\\
Execute c:\\php\\cli\\php.exe c:\\test.php will now cause an error about not finding mib files.

If you now copy the mibs to d:\\usr\\mibs and run the above command again, it will now work fine.

Effectively the drive the mibs has to be on is the same as the drive of the current working directory when you call PHP.

This isn't very useful at all, so after some digging around there seems to be an environment variable (MIBDIRS) you can set specifying where the MIBs are to be found.

After setting MIBDIRS=c:\\php\\mibs in the environment and placing the mibs there, everything seemed to work fine.

Note: If you need SNMP whilst using apache you may have to use the PassEnv command with MIBDIRS in your apache config, I haven't tried this yet.
Sean Boulter
09-Jun-2004 09:01
In addition to working on NT as the documentation says, SNMP works on XP if you have the snmp extensions enabled in the ph.ini file, point the ini file to the correct extensions folder, and copy the mibs from the full install package to c:\usr\mibs.
mcotner at mcotner dot com
25-Feb-2004 02:37
Hi all,
I had a need for mass polling of many devices and bulkgets were very desirable.  It turns out the change was simpler than I thought.

One word of warning, however.  The version is set in such a way that if I were to keep the changes to a minimum all operations that weren't v3 needed to be set to v2c.  This isn't a problem for us because we have no v1 specific devices.  If you're polling any v1 ONLY agents then you will have issues with this patch. 

Here's the patch.  I hope this helps.

Just save it to /tmp/patch
cd ext/snmp/
patch snmp.c /tmp/patch


--- snmp.c    2003-08-07 12:44:11.000000000 -0400
+++ /tmp/snmp.c    2004-02-24 22:03:22.000000000 -0500
@@ -405,7 +405,11 @@
         } else if (st >= 2) {
-            pdu = snmp_pdu_create(SNMP_MSG_GETNEXT);
+            int reps = 20, non_reps = 0;
+            pdu = snmp_pdu_create(SNMP_MSG_GETBULK);
+            pdu->non_repeaters = non_reps;
+            pdu->max_repetitions = reps;    /* fill the packet */
             snmp_add_null_var(pdu, name, name_length);
@@ -471,7 +475,7 @@
                             goto retry;
                     } else if (st >= 2) {
-                        if ((pdu = snmp_fix_pdu(response, SNMP_MSG_GETNEXT)) != NULL) {
+                        if ((pdu = snmp_fix_pdu(response, SNMP_MSG_GETBULK)) != NULL) {
                             goto retry;
@@ -579,7 +583,7 @@
     session.peername = hostname;
     session.remote_port = remote_port;
-    session.version = SNMP_VERSION_1;
+    session.version = SNMP_VERSION_2c;
     * FIXME: potential memory leak
     * This is a workaround for an "artifact" (Mike Slifcak)
thug at wolf359 dot cjb dot net
23-Apr-2002 03:06
Just a quick note on PHP SNMP instalation on Windows machines (IIS 5.x / Win2k SP2 etc...)
I had installed PHP into C:\PHP.. (standard stuff eh?)

Whilst I had followed the instructions for the installation of SNMP (put php_snmp.dll in extension_dir, uncomment entry in php.ini and all mib files located on the same drive where php was installed  (in this case c:\usr\mibs right?) I still ran into trouble like "Cannot find module (IP-MIB): At line 0 in (none)..." appearing on the bottom of every php that page I was viewing which existed on a drive other than C:\ drive...

It seams that the mibs dir (and content thereof) have to be located on every volume where a php file will be executed from.

For example :-
If you have a php file as "C:\INETPUB\WWWROOT\snmp1.php" then the coresponding "C:\USR\MIBS\*.*" has to be present (as mentioned in the install.txt).

Now here comes the catch...

If you have a "D:\INETPUB\PHPROOT\snmp1.php" file, then a copy of the mib files have to be in "D:\USR\MIBS\*.*" as well (the same mib files that are on the C:\ drive and same basic location <drive>:\USR\MIBS).

(Or in short : Copy the C:\USR dir (inc mibs dir) to *every* drive you intend to have .php files accessed from.  Done!)

Hope this helps other PHParians.

Keep up the *Great* work!!!