一、什么是PEAR
PEAR是PHP Extension and Application
Repository(PHP扩展与应用库)的缩写。PEAR为PHP用户提供了一套基本类库,在编写PHP程序时通过调用相关的PEAR类可以简化我们
的工作量。举个很简单的例子,比如我在程序中想把数据库中的数据通过Web页面的条件设定,抽取相应的数据转存到Excel表格里。要完成这个任务,在以
前可不是很容易办到的,还好现在有了PEAR,通过调用PEAR里面的Spreadsheet类,我们可以在很短的时间实现对Excel的写操作。长话短
说,还是开始尽快我们的PEAR安装之旅吧。
二、安装PEAR
首先介绍一下我的系统环境:
Windows 2000
WAMP1.4.3(Apache 1.3.33 + PHP 5.0.3 + MySQL 4.1.8)
大家可能对WAMP这个东西比较陌生,我就简单介绍一下:众所周知,在windows环境下要把这3个软件安装并且配置成功,还是需要费些劲呢。而WAMP(Windows Apache MySQL PHP)可以帮我们无需进行任何配置,只要到它的官方网站 下载一个大约20M的名为wamp5_1.4.3.exe的文件,然后直接运行安装就可以马上建立一套包含Apache/ MySQL /PHP/在内的使用环境,真是简单的不得了呀。
好啊,系统配置好后,下一步就让我们来安装PEAR吧。从PHP 4.04开始,PEAR随着PHP核心一起被分发。进入php安装目录,双击go-pear.bat,会弹出一个命令提示符窗口,显示如下信息:
Welcome to go-pear!
Go-pear will install the 'pear' command and all the files needed by
it. This command is your tool for PEAR installation and maintenance.
Go-pear also lets you download and install the PEAR packages bundled
with PHP: DB, Net_Socket, Net_SMTP, Mail, XML_Parser, PHPUnit.
If you wish to abort, press Control-C now, or press Enter to continue:
这里是说系统将下载并安装包括DB, Net_Socket, Net_SMTP, Mail, XML_Parser, PHPUnit在内的 pear几个基本的类库。直接回车执行下一步。按回车键后显示如下信息:
HTTP proxy (), or Enter for none::
设置HTTP代理服务器,如果你的IP地址是公网地址,就不需要设置,直接回车即可。按回车键后显示如下信息:
Below is a suggested file layout for your new PEAR installation. To
change individual locations, type the number in front of the
directory. Type 'all' to change all of them or simply press Enter to
accept these locations.
1. Installation prefix : D:\software\wamp\php
2. Binaries directory : $prefix
3. PHP code directory ($php_dir) : $prefix\pear
4. Documentation base directory : $php_dir\docs
5. Data base directory : $php_dir\data
6. Tests base directory : $php_dir\tests
7. php.exe path : D:\software\wamp\php\php.exe
1-7, 'all' or Enter to continue:
这里设置与php有关的系统环境,如果你采用WAMP方式安装的PHP,那么这里直接回车即可。按回车键后显示如下信息:
The following PEAR packages are bundled with PHP: DB, Net_Socket, Net_SMTP,
Mail, XML_Parser, PHPUnit.
Would you like to install these as well? [Y/n] :
含义为是否下载并安装DB, Net_Socket, Net_SMTP,Mail, XML_Parser, PHPUnit这些类库,当然是啦,输入Y然后回车。按回车键后显示如下信息:
Loading zlib: ok
Using local package: PEAR.............ok
Using local package: Archive_Tar......ok
Using local package: Console_Getopt....ok
Using local package: XML_RPC..........ok
Bootstrapping: PEAR...................(local) ok
Bootstrapping: Archive_Tar............(local) ok
Bootstrapping: Console_Getopt.........(local) ok
Downloading package: DB...............ok
Downloading package: Net_Socket.......ok
Downloading package: Net_SMTP.........ok
Downloading package: Mail.............ok
Downloading package: XML_Parser.......ok
Downloading package: PHPUnit..........ok
Extracting installer..................ok
install ok: PEAR 1.3.1
install ok: Archive_Tar 1.2
install ok: Console_Getopt 1.2
install ok: XML_RPC 1.1.0
install ok: DB 1.6.8
install ok: Net_Socket 1.0.5
install ok: Net_SMTP 1.2.6
install ok: Mail 1.1.4
install ok: XML_Parser 1.2.4
install ok: PHPUnit 1.2.0
*******************************************************************
WARNING! The include_path defined in the currently used php.ini does not
contain the PEAR PHP directory you just specified:
If the specified directory is also not in the include_path used by
your scripts, you will have problems getting any PEAR packages working.
Current include path : .;C:\php5\pear
Configured directory : D:\software\wamp\php\pear
Currently used php.ini (guess) :
Press Enter to continue:
下载安装完毕(注意:要保证你的电脑连到网上,以便PEAR下载所需类库),再次回车。显示:
The 'pear' command is now at your service at D:\software\wamp\php\pear.bat
** The 'pear' command is not currently in your PATH, so you need to
** use 'D:\software\wamp\php\pear.bat' until you have added
** 'D:\software\wamp\php' to your PATH environment variable.
Run it without parameters to see the available actions, try 'pear list'
to see what packages are installed, or 'pear help' for help.
For more information about PEAR, see:
Thanks for using go-pear!
* WINDOWS ENVIRONMENT VARIABLES *
For convenience, a REG file is available under D:\software\wamp\php\PEAR_ENV.reg
.
This file creates ENV variables for the current user.
Double-click this file to add it to the current user registry.
请按任意键继续 . . .
按任意键后,该命令提示符窗口关闭。最后进入apache安装目录,打开php.ini,
找到:
;;;;;;;;;;;;;;;;;;;;;;;;;
; Paths and Directories ;
;;;;;;;;;;;;;;;;;;;;;;;;;
; UNIX: "/path1:/path2"
;include_path = ".:/php/includes"
;
; Windows: "\path1;\path2"
;include_path = ".;c:\php\includes"
将最后一行改为:include_path = ".;d:\software\wamp\php\PEAR"
然后保存退出。
注意:d:\software\wamp\php\PEAR这个目录要根据php\PEAR实际的安装目录进行填写。
好啦,到这里PEAR已经安装完毕了,是不是很简单呀,基本上运行go-pear.bat后,一路回车就可以了。
三、安装并使用PEAR的Spreadsheet_Excel_Writer库生成Excel表格
没有使用过PEAR的朋友可能对PEAR的具体应用还不是很了解,这里我们举一个简单的例子来进行说明。首先进入到php的安装目录下。执行如下命令:
pear download OLE
pear download Spreadsheet_Excel_Writer
说明:pear download package代表下载相关的package。
pear install OLE-0.5.tgz
pear install Spreadsheet_Excel_Writer-0.8.tgz
说明:pear install packagename代表安装相关的packagename。关于pear的更多命令以及含义可以执行pear来查看。安装完毕后执行pear list,如果看到如下2行,就说明安装成功了。
OLE 0.5 beta
Spreadsheet_Excel_Writer 0.8 beta
然后我们在www目录下建立一个test.php文件,内容如下:
include_once "Spreadsheet/Excel/Writer.php";
$xls =& new Spreadsheet_Excel_Writer();
$xls->send("test.xls");
$sheet =& $xls->addWorksheet('Test results');
$sheet->write(0,0,12);
$sheet->write(0,1,"Hallo, ich bin ein Text");
$format =& $xls- >addFormat();
$format->setBold();
$format->setColor("green");
$sheet->write(1,1,"Hallo, ich bin formatiert",$format);
$xls->close();
?>
然后直接运行,此时IE浏览器会弹出一个请求下载test.xls文件的对话框,下载后打开就可以看到生成的Excel表格文件了。具体的
Spreadsheet_Excel_Writer生成Excel表格格式语法大家可以参考pear的官方手册,这里就不多说了。好啦,我们的PEAR之
旅到这里就结束了,您是否已经开始对pear感兴趣了吗,那还不去试试看!