I just gave a talk today about Unit testing PHP code using PHPUnit in Drupal Camp 2015.
I have recently added Unit testing as part of my development workflow and it has helped me a lot as a developer and has even changed the way I think about code.
In this talk I talked about how to get started doing Unit testing in PHP and about some of the tips that I learned as I started doing Unit testing.
I have decided to drop support for PHP 5.2 in my WordPress plugins. I have thought about it for quite sometime and even hinted about it in the last couple of my posts.
I am writing this blog post to explain the reasons behind my decision. The following are the two main reasons why I decided to drop support.
If you have used my WordPress readme generator you would have noticed that I have a “Save As” button, which when pressed allows you to download the file which was just generated by the PHP code. (In most browsers this opens the save file as dialog box)
Recently one of the readers of my blog asked me how to do this in PHP and I was about to send him the code snippet. But then I thought of writing a blog article so that it will be useful for others as well.
So here we go.
The code is mostly self explanatory. The important part in the above code snippet is the “content Disposition” header.
Also since we are sending headers to the browser, we should not have started the output before this code executes. Otherwise we might get a “header already sent” error.
Recently for a project which I was working on, I wanted a way to expand urls shorted by url shorteners.
After a couple of web searches I found an API site called LongURL. This service was really slow and I had to look for alternatives.
I thought of writing my own using curl and then parse the headers. I was searching for the curl functions in PHP Manual and I found a simple but not so famous function called get_headers().
This function is all we need to expand the urls. The following function expand_url() takes a short url and will return the longer version of the url.
Recently, I wanted to play around with some stuff which is available only in PHP 5.3.x (more about it later in a separate blog post) and so was looking for a way to install it on my Ubuntu server, where this blog is running.
After poking around a bit, I found that Karmic Ubuntu hasn’t upgraded to PHP 5.3.x yet and the only way to do is to compile from PHP source. Even though I am pretty comfortable doing it, I didn’t wanted to do it, because it is very difficult to upgrade at a later point in time.
I was continuing my research and then found that it is in fact possible to install PHP 5.3.x though apt-get or aptitude. I thought of documenting it here, so that it would be useful for others who want to do the same thing.
Adding dotdeb to the source list
First you should add dotdeb repository to your apt-get source list. Add the following two lines to your /etc/apt-get/sources.list file
sudo vim /etc./apt-get/sources.list
Adding dotdeb keys to keyring
Dotdeb packages are GPGsigned. Issue the following commands to add the keys to key-ring
Install PHP5 packages
Then issue the following command to retrieve the updated package list. I am using aptitude here; you can use apt-get as well.
sudo aptitude update
sudo aptitude upgrade
And then you can install PHP5 packages (and modules) using the normal install command.
sudo aptitude install php5 libapache2-mod-php5
Installing php5-dev package
The above method will install all php5-* packages, but php5-dev has some dependency issues with libtool packages. In order to solve that you have to manually install libtool v1.5.26. To do that use the following commands.
Now it’s time to enjoy the new features that are available in PHP5.3 🙂
Just wrote quick some code sample to show how to access YQL (Yahoo Query Language) queries from PHP.
You can use the following code sample to fetch results from YQL from any standard PHP file. You would require the curl extension to be installed. If you are behind a proxy, uncomment the 3 lines and replace it with proper proxy values
If you have the Yahoo social SDK installed and you want to access results from YQL, then you can use the following code sample. Replace the text consumer_key and consumer_secret with correct values.
Yesterday, I talked about using memcache in PHP in the Bangalore PHP meetup.
There were lot of good questions and the discussion which followed after the presentation was very good. Thanks for the organizers for putting together a great event.
I have uploaded the slides which I used for the talk to my slideshare account.
I just gave a talk in Bangalore PHP Meetup about ways in which we can use WordPress as a platform. The following is the slide I used and a little write up about the talk.