Categories
PHP Python

A Python Script Every PHP Developer would love

PHP’s mail() function is one of the best things in the language. It is also the item most people suffer with while working locally. So whats the problem? The problem is we usually don’t have a SMTP server installed on our local machines (while we do in our production servers). PHP doesn’t do magic, it can’t send the emails without a SMTP server. Since you don’t have one, don’t go nuts seeing PHP can’t deliver your email 🙂

Remembering this problem, I spent 20 minutes hacking a Python script that does the magic for your PHP setup. The Python script creates a dummy SMTP server and listens to the port you select (defaults to 25). Keep the script running while using the mail() function in PHP. You shall get detailed yet readable mail data printed on your screen.

So isn’t there the nice smtp4dev tool and mail servers like postfix? Why re-invent the wheel? Ah, good question. Postfix is cool and it actually sends the email depending on your configuration. But it’s not probably a good option for debugging purposes. Do you want your mailbox filled up with test emails? 😉 Also AFAIK, it has no way to run on Windows. And I admit, sometimes emails are delivered quite late while using postfix. On the other hand, smtp4dev is a nice tool for similar purposes but it’s not cross OS either. It’s Windows only. Besides, it crashes if you try to send emails in French (probably an issue with Unicode). My python script solves this issue. It doesn’t fill up your mailbox, it handles French (and other languages with special chars) and most importantly runs on all the major OS.

Enough talks! So where’s the script? It’s right here:

How do I run it? First create a new file named: capture_email.py and copy-paste the codes into that file. If you’re on Linux or Mac, you already have Python 😀 Just type this command and hit enter:

If you’re on Windows, download a version of Python from http://python.org and install it. I recommend using Python version 2.5+ but less than 3. Python 3 is not supported. Double click on the file to run it.

Now that the script is running, try sending an email with mail() in PHP. Keep any eye on the Terminal/Command prompt for the output.

If something goes wrong, please let me know. Hope this script will be useful for the PHP devs out there. 🙂

Categories
PHP

MSSQL Server and PHP: Getting Started

Well, before we get started lets look at what we shall need:

1) MS SQL Server 2008 R2 Express Edition
2) MS SQL Server Management Studio
3) A WAMP Setup
4) The MS SQL Driver for PHP

You can get the first two products in your desired package from this link: http://www.microsoft.com/express/Database/InstallOptions.aspx

Since we need both the database engine and the management studio, we should pick the 2 in 1 pack. Now install the tools. While installing the SQL Server, at a point it’ll ask for an “Instance” name. Use the default instance. If you already have MSSQL Server with Visual Studio, please note that I had issues with the VS installation (SQLEXPRESS instance). I couldn’t connect to that instance even from Visual Studio. So I had to install another instance with the default instance name which worked for me. If you don’t have a previous installation, then you don’t need to bother about it. Just remember to set the instance to default instance name (MSSQLSERVER).

After the management studio and the server is installed. We need to install SQLSrv – a driver written by Microsoft for PHP connectivity to SQL Server. If you’re using PHP on Windows, I recommend this one in contrast to the old mssql.dll driver. A good article about the difference of the drivers can be found here: http://blogs.msdn.com/b/brian_swan/archive/2010/03/08/mssql-vs-sqlsrv-what-s-the-difference-part-1.aspx

OK now, so download SQLServ from this URL: http://www.microsoft.com/downloads/en/details.aspx?displaylang=en&FamilyID=80e44913-24b4-4113-8807-caae6cf2ca05 🙂

After downloading double click to extract the contents. It contains different drivers for different builds of php. Assuming that you already have a WAMP server setup, view your phpinfo() and look for “PHP Extension Build”. In my case it reads – “API20090626,TS,VC6”. That means I have to pick the Thread Safe (noted as “ts”) version which was compiled using VC6. Given that my PHP version is 5.3, I chose: “php_sqlsrv_53_ts_vc6.dll . Choose the one appropriate one for your PHP build and drop it in the “ext” folder. I shall not cover how to load an extension since as a php dev you already know that. Edit your php.ini, enable the extension and restart the WAMP server.

Now, launch SQL Server Management Studio and login using Windows Authentication. Right click on the server name “(local)” and go to properties. You shall have a window like this:

From the “Security” section, enable “SQL Server and Windows Authentication mode”. This will let other tools like PHP to connect to MSSQL Server which can not use Windows Auth easily. After you press “OK”, the SQL server will need to be restarted. After restarting, again log into the management studio. Expand the “Security” node under the server name. Right click on “Login” and select “New Login”. Type a login name, select SQL Server authentication and create the user.

Once you are done with these steps, now we write some codes to see what we’ve done so far:

Output:

I copy-pasted the codes from the SQLSrv manual. Ah, I forgot to tell you the driver has a nice manual for learning the available APIs. The driver also has a PDO version. If you’re a PDO fan, don’t forget to check out the PDO driver and write some review.

Categories
PHP Work

PhpTube: A PHP Class to get download links from Youtube Watch URLs

THE API HAS BEEN BROKEN FOR A LONG TIME, AND I DON’T WANT TO FIX IT SINCE THERE ARE BETTER ALTERNATIVES LIKE “youtube-dl”.

Let me get to it straight: I wrote a php class that takes a youtube watch url (the typical url to watch a video, the url has a “watch” GET parameter in it) and returns the download links for different available formats of the video. Please note that these links will be valid for the requesting IP address. That is you can not download the videos from other IP addresses except the only one IP adress that actually originated the download request. So if you put it on your server and send these links to your users, they’ll certainly not be able to download from these links since their IP addresses will be different from your server IP address. In that case, you should first download it to your server and then link to those downloaded videos.

You can download the source codes from my github repo: https://github.com/masnun/phptube 🙂

An example:

Output: