Categories
PHP

PHP: Setting the include_path for quick includes

PHP being the first language I loved and used for a long time, I now use it for all sorts of purposes. I also love and use Python but very often I end up coding in php because I am more habituated to this language than the beautiful python 🙂 Again for some simple tasks, php is loads better than Python. Just imagine you need the md5 hash of a string. Will you import the hashlib first, then go through the md5() and hexdigest() methods ? Nope. I will simply fire off a php interactive shell and use the built in md5() function with an echo.

One of the major problems I was facing that all the external libs I use, I have to copy them into my current working directory or have to use some other techniques to import them into my current script.

To get a permanent work around this problem, I decided to use the include_path directive in php.ini 🙂 I created a directory named “phplib” under my home directory and put all the libs in there.

Next, I opened up /etc/php5/conf.d/ with root access and added a new file named phplib.ini with the following contents:

Now, whenever I need a library loaded, I just type :

That imports the mailer class, even on my interactive php shell 🙂

PS: Well, since the php interpreter always has your home directory as the CWD, you could just use include(“phplib/mailer.php”); in this case. But what about the php module running with apache? The include_path affects that one as well. It helps! But also it has a negative side. Make sure you upload the respective libs to the web server before you deploy your web application on the production box! 😀

Categories
PHP

A PHP Mailer Class

UPDATE: Added application of the basename() function to the attached filenames to get the filenames only.


This php class lets you attach multiple files and send emails via Postfix without digging into the messy headers 😀

Example USE:

Happy Mailing! 😀

Categories
PHP

Google’s Text to Speech API : A PHP Wrapper Class

It’s really amazing how Google is providing cool APIs for every this and that! And here comes another one that impressed me 😀 I am talking about the Text To Speech API by Google. It’s fantastic! I wrote a php wrapper class that would help you create mp3 files from texts 🙂 Of course, using Google as the medium!

Here’s the source code:

And here’s demo :

You can alternatively pass the text to the constructor of the object like this:

That is simple, isn’t that? Hope you like it!