Monday, July 26, 2010

3 Awesome APIs Everyone Should Know

These days, application programming interfaces (APIs) are ubiquitous. Without them, Web 2.0 would not be what it is. Developers would not have any means to hook into other web services and create useful mashups. The overall integration of content on the internet would be next to nothing. You would not be able to post a link to Facebook about a YouTube video (from YouTube) or update your status on Twitter from your iPhone. We rely on APIs heavily for much of what we do on the internet. Here is a brief overview of some neat APIs every developer should be aware of:

1. Facebook API 

If you have not heard anything about Facebook's different APIs, you must have been living in a hole for the past few years. Facebook has become the de facto social integration tool. With over 500 million active users, Facebook has become an unstoppable force of content generation and user interaction. I have even heard some people say that Facebook is becoming the internet. The company has strategically infiltrated every corner of the internet with "Like" buttons and "Login with Facebook" buttons. This is not an API that any developer can continue to ignore. Seriously. It is "do or die".



2. GoWalla API 

GoWalla.com's API is a location-based web service. It provides developers with a way to convert between latitude/longitude coordinates and nearby establishments. GoWalla really hit big once it was debuted at the SXSW conference in 2008. You give GoWalla a latitude, a longitude and a radius (in miles) and it returns a feast of nearby establishments in JSON. The reason why I like this API so much more than GoWalla's main competitor, foursquare.com, is the type of data GoWalla gives access to and the fact they use a REST approach.




3. Google Maps API

You can integrate a map of anywhere into anything now thanks to Google Maps. Yes, there are mapping APIs available (Bing and MapQuest). There are drawbacks and benefits to each. I have the most experience with Google Maps due to a project I worked on recently. The project required a robust map and the supplying service to allow many thousands of hits a day without incurring a cost (initially, at least). Bing's terms of service stated that the number of requests per day were closely monitored and highly restricted. MapQuest's API (see Documentation) was not flexible or stable enough to handle the complicated requests we were making from our service.

Google Maps API came out a strong contender and never disappointed. Google offers some extremely useful functionality and the service was very reliable.

Monday, July 12, 2010

Now Anyone Can Make Android Apps

A new tool from Google, named App Inventor for Android, promises to make developing applications for Google's Android platform easier than ever. App Inventor provides the user with a graphical approach to constructing apps.



Traditional development normally entails the Eclipse IDE, the Android SDK, and a lot of Java code. With App Inventor, you do not have to worry about any of this; you do not even need to be a developer. Most of the time, when an organization abstracts away all of the complexities of the product, it also inherently reduces its functionality. This is not the case with App Inventor as it still gives the user access to the device's GPS, phone features and camera.

App Inventor is built off of MIT's Java Open Blocks, a framework for visually representing Java code. This is the bit of the software that allows non-programmers to use it. Currently, this tool is only available to educators. If you qualify, I highly recommend checking it out and letting me know what you think!

Saturday, July 3, 2010

Whoa! Now that is a Road Trip!

If you know me, then you know that I love driving. My friend recently came across one hell of a road trip. Be sure to take a look at the map slightly zoomed in. It has about a billion turns and some nightmare-ish chicanes and hairpins.


View Larger Map

So if you happen to be looking for an awesome road to drive and are in the neighborhood of central Romania, let me know how it is! This TopGear video is probably as close to driving on this road as I will get. Alas.

Friday, July 2, 2010

The Town That Was

I recently came across this sad video on Hulu. "The Town That Was" is a story about a small town in Pennsylvania whose coal mine caught fire in 1962 and continues to burn to this day. The mine fire drove out thousands of residents and caused the permanent closure of part of a state route. Despite many attempts by the government to save this quaint little town, it is all but abandoned save the very few who stayed.

Learn more about Centralia, PA

Friday, June 25, 2010

PHP Session Issues

I recently had a ridiculous problem with a user authentication script on a site I was making. Upon first loading the page, a user would have to execute the login script twice for the account to be authenticated. It literally puzzled me for weeks.

Eventually, I had determined that there was something about the PHP session variables that was not being set. I had no other problems elsewhere I employed PHP sessions; just the login script. Further investigation revealed that the real issue was with the cookie the PHP session creates.


Important Note: When session_start() is called, a cookie is created and the session id is stored in it. For more on PHP sessions, check out this article from php.net. 


Here is how it breaks down:

  1. The user points their browser to http://url.com/index.php
  2. The user attempts to log into the site.
  3. The authentication script calls session_start() and logs the user in, setting a state variable $is_logged_in to true.
  4. When the script ends, it redirects the user to a page using header('Location: http://www.url.com/somepage.php').
  5. The $is_logged_in = true in the PHP session does not carry over from the authentication script.
Why?

The PHP session data cannot be accessed by somepage.php because when the authentication script called session_start(), a cookie (learn more about PHP and cookies) was created and assigned to the "http://url.com". When the redirect completes, the current domain is now "http://www.url.com". It is a simple mistake, but devastatingly annoying.

Solution 1: Using a PHP Configuration File

There are a number of ways to remedy this issue. I chose a preventative approach which requires an .htaccess file. 

If you have access to (check with your hosting provider if you are unsure) your PHP Configuration File, php.ini, you can add a directive that forces PHP to always issue cookies to the top-level domain and all subdomains. Add the following line to your php.ini file: 

session.cookie_domain = .url.com 

For more information, read this. Please note the period before the "url.com". It is important. Also, note the lack of single or double quotation marks.

Solution 2: Inline Directive

Instead of having to modify your php.ini file, you can set the cookie domain inline with a particular script using the ini_set() function (see PHP manual). Add the following line to your PHP script: ini_set('session.cookie_domain', '.url.com'); Take note again of the period before "url.com".

Solution 3: .htaccess RewriteRule

This is the method I chose because it is an inherently proactive approach. It requires an .htaccess file and the following code:

Options +FollowSymlinks
RewriteEngine on

RewriteCond %{HTTP_HOST} ^url.com [NC]
RewriteRule ^(.*)$ http://www.url.com/$1 [L,R=301]

The first line tells Apache to honor symbolic file links. If you have a file symbolically linked to another file and get a HTTP 403: Forbidden when trying to access it, it may be because your .htaccess file lacks this line. The second line tells Apache to enable URL rewriting (see Apache manual). This is the first step to fixing the matter at hand. The third line is a rewrite condition (see Apache manual). If the condition is met, it executes the following rewrite command on the fourth line. The RewriteCond condition verifies that the base domain is exactly url.com. If the condition is met, line four takes the URI and appends it to the string "http://www.url.com/", forcing the www subdomain. 

Problem solved.

Thursday, June 17, 2010

Physics can be fun!

If there ever was a person who cared about Physics more than life itself, it would be Julius Sumner Miller (1909-1987). Take a look at his Wikipedia biography. These YouTube videos are proof that even the most complex physics concepts can be made attainable. Professor Miller produced quite a number of episodes for this show, most of which can be found on YouTube.


I idolize this individual for a few reasons. His way of speaking is something of a lost art. His word choices are enchanting and, much of the time, downright hilarious. When he explains how the ping pong ball defies gravity in the glass funnel when turned upside down due to a low-pressure field, it almost seems as if he is revealing the secret behind how he made the elephant disappear.

YouTube Videos
Probably the funniest and strangest episode
Inertia
Energy and Momentum
Bernoulli Principle

Sunday, June 13, 2010

Creating Vanity URLs with .htaccess

I had to do this for a project website I was working on recently. We knew we wanted the user's profile to be accesible by entering the www.url.com/username. This was, more or less, created automatically when the user created a profile. I brainstormed dozens of ideas, all of which were either not secure or overly complicated. Finally, the answer came to me: .htaccess and regular expressions.

Our server is Linux-based and runs Apache for the web server. Because we use Apache, we can use .htaccess...and it is a beautiful thing.

An .htaccess file is used for a number of reasons:

1. To redirect a user to File B when they try to access File A
2. Restrict/allow access to particular directories or files (with a little help from .htpasswd)
3. To beautify long, cryptic URLs

The way in which .htaccess accomplishes #3 is via mod_rewrite. Here us some more information regarding this Apache module. To enable mod_rewrite, add the following two lines to .htaccess file:


Options +FollowSymlinks
RewriteEngine on

For the sake of example, let us say that the PHP file that handles displaying a users profile is named profile.php. Furthermore, a username may be comprised of numbers, letters (upper and lower case), periods, hyphens and underscores. Adding the following rule:

RewriteRule ^([A-Za-z0-9\._\-]+)+[^\.php]$ profile.php?&uri=$0 [NC]

 to the .htaccess file will rewrite http://www.url.com/username to http://www.url.com/profile.php?uri=username. An important note about the rewrite is that http://www.url.com/username is what is still displayed in the browser's address bar. Thus, a vanity URL!

An explanation of the regular expression used in the rule is beyond the scope of this post. Read more about using regular expressions with mod_rewrite.