Wednesday, April 1, 2009

Installing PEAR packages on WAMP

Well, after getting caught out recently with PEAR on a client installation, I thought it a good move to write a short tutorial for anyone who needs to install PEAR packages on their WAMP installation.
To start with, for the purposes of this blog, PEAR is a framework for implementing code libraries in PHP. In other words, it prevents you from having to re-invent the wheel - always a nice thing!
The WAMP installation installs PHP, and at the same time installs the basic PEAR framework. There is a directory called PEAR inside whatever folder contains PHP - on my particular installation the structure is php5.2.5\PEAR. Taking a peek inside the PEAR folder will reveal it populated with the basic framework for PEAR. Most of this you can ignore - I’m not going to detail installing this here - that seems to be well covered. I just couldn’t find out how to actually enabvle the damn packages when I started, so I hope this is useful!
Checking Configuration
This is THE place where things can go PEAR-shaped, if you’ll allow the pun. The PEAR folder must be included in the includes path within PHP.INI. Open PHP.INI and find the line that starts with :
include_path =
or, if commented out
; include_path =
You need to add the path to your PEAR folder to the end of this string. For example:
include_path = “.;e:\wamp\bin\php\php5.2.5\includes;e:\wamp\bin\php\php5.2.5\PEAR”
Now - it is CRUCIAL that there are NO spaces after or before the semi-colons that separate paths in this string. If there are, quite simply, PEAR will not work. Simple as that - any incldue path after such a space will NOT be searched by PHP.
Once you have modified the PHP.INI file, save it and restart your WAMP installation.
Installing a package
First of all, identify the package(s) that you want to install. To do this, take a look at http://pear.php.net/packages.php, which lists the available packages for PEAR. Identify the ones you’re interested in - for the same of example, let’s deal with a Mailer package.
Click on the relevant section of the Package list, and you’ll see a list of available Mail packages. Select the one of interest - choose the package ‘Mail’ and click on it. This will then display a page of details about the Mail package - and also the links to download, documentation, etc. Now, there are two ways to download stuff, depending upon whether you have access to the command line program for PEAR or not. This blog entry will assume you have - I’ll post the alternative instructions elsewhere.
To start with, make a note of the package name - ‘Mail’.
Check the Dependencies
Click on the ‘Download’ tab - you will see an entry someway down the page for ‘Dependencies’. These are PEAR packages that you need to install for your chosen package to work. Make a note of the dependencies - in this case, Net_SMTP. Now click on that dependency, and check it’s dependencies - do this and you will see Net_Socket and Auth_SASL listed. The latter is optional, so I won’t bother with that. There are two other packages listed - however they’re part of the PEAR framework.
So, we now have a list of packages to download and install. These are:
Mail, Net_SMTP, Net_Socket. (We’ll ignore the Auth_SASL one for this example).
Whilst you can automate the acquisition of dependencies, it’s educational to do it ‘the hard way’ at first. :) So that’s what we will do!
Start a Command Window
From your Windows Start menu, select the ‘Run’ entry, and enter ‘cmd.exe’. Now change directory to the PHP directory. On my machine this is:
E:\wamp\bin\php\php5.2.5
Once in there, type in pear list. This will list the currently installed pear packages. If it’s a new PEAR installation, there will be 3 packages listed 4 packages listed - PEAR, Archie_Tar, Console_Getopt and Structures_graph.
Now, type in pear update-channels. this connects to the PEAR repository - where the PEAR files live - and updates your local installation with data needed to get packages.
Now, type in:
pear download Mail
pear install Mail
The former command gets a ZIP file containing the Mail package from the PEAR repository, and the latter will install the mail package. Note the messages displayed on installation - the software warns you that you haven’t got the dependencies, and gives you a suggestion of a method of downloading the dependencies - which we’ll now use to get Net_SMTP an it’s dependencies.
We’ll also do this installation slightly different to the first one - thus showing there is more than one way to install a package. Type in:
pear install -a Net_SMTP
No download this time - this will go off and get Net_SMTP, and all of it’s dependencies, and install them - all in one fell swoop! The -a switch tells PEAR to get all the dependencies.
The files will be installed in to the PEAR folder. Sometiems a PHP file will be placed in the folder itself - as happens with the DB package. Other times a new folder will be created - like for Mail - and the PHP files placed in the new folder.
Testing the mailer.
Here is a piece of sample code to put in to a PHP file:require_once “Mail.php”;
$from = “Sender ”;
$to = “Recipient ”;
$subject = “Hi!”;
$body = “Hi,\n\nThis is a test!”;
$host = “mail.example.com”;
$username = “smtp_username”;
$password = “smtp_password”;
$headers = array (’From’ => $from,
’To’ => $to,
’Subject’ => $subject);
$smtp = Mail::factory(’smtp’,
array (’host’ => $host,
’auth’ => true,
’username’ => $username,
’password’ => $password));
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
echo(”

” . $mail->getMessage() . “

”);
} else {
echo(”

Message successfully sent!

”);
}
?>
Set up your own parameters for the mail server and the authentication required - username and password, and the address of the recipient - and if you have successfully installed PEAR Mail then you should get a mail sent. If you get any messages back refering to ‘No such file or directory’ then the chances are that the configuration of PHP.INI is a little screwed up. And there you have it!
orignal article : http://www.joep.communityhost.org.uk/?p=30

No comments:

Post a Comment