In this post I will show you how to use my updated version of the foursquare-php class to display your latest foursquare on your page and blog. I will go through the changes induced by using Foursquare API v2 and what are the new awesome features introduced in this version: getting previous checkins and using the lib as the JSON backend.
A few weeks ago Foursquare made mandatory to use their new API which introduce a significant change in the authentication system: instead of using the user login and password to access the data, it is now required to use the OAUTH protocol. While this is a great thing for security (I am a big proponent of OAuth) because the new version only need to store a OAUTH token and not your credentials, it make the setup process slightly more complicated as we need to generate this token. Don’t worry ! After experimenting with a couple of setup flows, I came up with a process that will make this a painless as possible for you guys.
Installing the library
Getting things up and running require you to follow the following three steps:
1. Download the latest version of the Foursquare-php library from its code repository: http://code.google.com/p/foursquare-php . Put your copy in your website directory. Make sure your webs-server can read it (chmod 755).
2. Generate your OAUTH token. To do so go to the page https://elie.im/foursquare/ and click on the green boutton that said : get my oauth token as visible in the following screenshot:
Clicking on this button will redirect you to the Foursquare website that will ask you if you want to authorize the fq-php app to access your data as visible on the screenshot below. I choose the name 4sq-php because you can’t use foursquare in the application name.
Once you have allowed the fq-php application you will be redirected to the page https://elie.im/foursquare/callback.php that will display your’s OAUTH token. as visible in the screenshot below. All you have to do is to copy it somewhere and you are done !
Using the library
Now that we have the library installed we need to use it by creating a page that use it Here is a simple example of how to use it.
<?php require_once 'foursquare.php'; //including the class $fq = new fourSquare("2SMxxxxxxxxxxxxxxxxxxxxxxxx"); //fetching the checkins data ?> <div id="foursquare" style="text-align:center"> <h2>Last known location:</h2> <!--displaying the foursquare logo for the venue type--> <img src="<?php echo $fq->venueIcon ?>" /> <!--displaying the venue name and the venue type--> <?php echo $fq->venueName ?> (<?php echo $fq->venueType ?>)<br/> <!-- displaying the venue address --> <?php echo $fq->venueAddress . ", " . $fq->venueCity . ", " . $fq->venueState . ", " . $fq->venueCountry ?><br/> <!--Displaying the map--> <img src="<?php echo $fq->getMapUrl(250, 250) ?>" /><br/> <!-- displaying the user comment--> <i><?php echo $fq->comment ?></i><br /> </div>
In line 2 we include the foursquare-php library from the same directory (so don’t forget to change the path if you have saved it in a different place). In line 3 the class is initialized using as a parameter the OAuth token that we generated in the setup phase (2SMxxxxxxxxxxxxxxxxxxxxxxxx in the example). During this initialization the lib will fetch the checkins data from Foursquare and parse it.
The remaining lines are used to format and display the checkin data. This demo code will display something like this if everything went well:
In future posts, I will show you how to use the lib to display your last fives locations and use the lib as an AJAX backend. Let me know in the comment section what you think of this new version and if you have any questions.