If you want to build a useful app quickly, you can’t go past a mashup. Take data from someone else’s service, come up with a useful way to mix it up and present it to your users in an innovative manner. Even better, as my recent PHP Site Search tutorial showed, you can build a mashup in just a few lines of code. But how do you find useful web services/APIs, and how do you process the data they provide?
Finding APIs and web services is really quite easy. If you regularly use a web application - for example, I’ve often used Twitter - check if the application provides an API (Twitter has quite a comprehensive one). Next, if you’re looking for data on a particular topic, try searching for it. A quick search for “map api” returns all kinds of useful results. Finally, if you just want to take a look at the internet’s available APIs, head to Programmable Web; they have by far the most comprehensive database available, and it’s fully searchable.
The easiest way is a really PHP-friendly APIs that provides serialized PHP. You can just fetch the URL with file_get_contents(), and run the output through unserialize(); in two lines of code, you have your data ready to work with.
However, most APIs won’t offer serialized PHP output; some might offer JSON, or JavaScript Object Notation, which is somewhat like PHP’s serialized data (although JSON is integrated in the JavaScript language). Others offer XML gateways, while some even provide SOAP, a (rather complicated) means for computers to talk to each other natively. You really have three options:
1. Interpret the output of the API or web service with a PHP-based parser, such as an XML parser (which you can easily find on phpclasses.org) or a SOAP client (try nusoap). 2. Use PHP sample code provided by the API/web service owner - e.g. this Google Checkout PHP sample. Some even provide PECl extensions. 3. Use a third-party library to access the service. For example, people have written PHP classes that allow you to interact with the Flickr API. PHP Classes.org has hundreds of these, and others are just a web search away.
Of course, if you just want to get started right now and quickly add some functionality to your application, try these:
Have a look through these APIs and you could be building a mashup quickly in no time.
Akash Mehta in PHP | No Comments »
In part 1 of this series, I looked at the web browsers and (Windows) IDEs. Today I’m going to look at a local development server and its PHP configuration, as well as some of the IDEs/editors available for Linux, especially the cross-platform options.
(more…)
Akash Mehta in PHP | 4 Comments »
PEAR, the PHP Extension and Application Repository, contains hundreds of freely available packages that can be reused in your application. Packages usually come with various functionality within one or more classes, and the PEAR coding standards make sure packages follow the same general style for ease of implementation. Best of all; they’re all entirely open source, from MIT to GPL.
However, many of these packages are bloated, slow and full of specialised features that you may never want to use. Sifting through the repository is also a challenge; a basic category system is in place, but it’s hard to tell what you want when you don’t know what’s available. Here are some gems from the PEAR repository that you could really find useful. (more…)
Ever wondered how long your PHP scripts were taking to execute? Or maybe you’ve broken the maximum execution time limit on your server, as defined in your php.ini’s max_execution_time directive. Well, dealing with PHP script execution time is actually quite easy. In this mini tutorial, I’ll show you how to measure your PHP script execution time, include monitoring at different stages of the process, and work around your max execution time limit. (more…)
Developing in PHP isn’t a textbook science. There’s skill to it, and that skill comes from experience. Now, you could try your hand at every single PHP development challenge out there, or you could learn from the experiences of others, widely published on the web in blogs and developer portals. Here are seven websites you should visit frequently to become a better PHP developer.
Akash Mehta in PHP | 1 Comment »
I was recently working on an enterprise project in which I needed to detect the text inside a Word Document. Now, I could have got rid of all the non-standard characters from the .doc file and hoped I got something reasonable at the end. I could have tried to run Word 2007 via command line to save the file as a .docx. Or I could just talk to any copy of MS Word via COM and have it do all the dirty work for me.
Naturally, I chose the latter. Here’s ten lines of code that do just that.
Akash Mehta in PHP | 2 Comments »
UPDATE: Part 2 now available.
After you’ve been coding PHP for a while, you start to work out what the tools of the trade are, which applications can help you work more effectively, and essentially how to get things done. While developing, your environment is crucial: not having the right tools to do your job can be a serious impediment on productivity. In this new series, I’ll help you bring together the ultimate PHP web development environment. (more…)
Toby Somerville recently blogged about giving visitors a rough time - instead of displaying exact timestamps, providing a rough written approximation of the time.
Toby suggested that we generally communicate in approximate times, but on the web we usually work with exact timestamps. His idea was that by displaying the time as you would describe it verbally, he could better communicate the time in a visitor-friendly way.
After reading his post, I understood the merits of the approach, but it struck me that a relative time might be a little more useful for many situations. For example, in a fast moving discussion, a short timestamp (e.g. 8:30 AM) as well as a verbal summary of how long ago the time was (e.g. “4 hours ago”, “20 minutes ago”) are most useful to the user. In fact, this is precisely what Gmail does:
A common feature of Gmail’s timestamp system is approximation, similar to what Toby was getting at. For example, as you approach 60 minutes in the hour, Gmail seems to switch from “x minutes ago” to “1 hour ago”, even if you’re not quite there.
Now, I figured this could be achieved very easily in PHP - and it could. After the jump, here’s the code:
First, we had variable naming conventions. Then we got classes and functions / methods, so we got general naming conventions. With PHP 5.3 introducing namespaces, it’s time to say hello to namespace naming conventions.
Namespaces were introduced to solve the problem of naming clashes. Once upon a time, all your code lived in one, two, maybe three files, and you could see and know exactly what was going on. Then applications started expanding, and OOP came along. Suddenly there were hundreds of functions, classes and methods in your application, not to mention variables and properties.
Here’s an interesting sales pitch. Using a PHP Framework? Cool. Just make sure you add Zend.
The Zend Framework is a fairly standard, (optionally) MVC PHP application framework. It comes with all the usual functionality; request routing, database access, templates (through view files) etc. It also comes with a lot of extra functionality that you might find useful in your application, including some of the best web services libraries available, especially when working with Google’s APIs.