foursquare

Display your latest Foursquare checkins on your blog using Foursquare API v2 and PHP

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?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.

If you like this lib you can donate and share it on Facebook/Twitter/Google+ :) You can also follow-me on Twitter and Google+ to get the latest updates :)

Liked this post? Share it!

  • http://twitter.com/JerrydeWit Jerry de Wit

    Hi, i love this script and i’m using this with love! Can we expect more functions then Ajax backend and last 5 locations? Something like venue’s like the same in a mile of distance or something ^^? *sorry, im Dutch so English is not my first language*

    • https://elie.im Elie Bursztein

      Hey Jerry,

      I am not sure. If there is a request for something specific I will try to do it. I think the next thing, I will do is a WordPress widget :)

      • http://twitter.com/JerrydeWit Jerry de Wit

        Thanks for your quick reaction, but u also gonna work on the script isn’t ^^? I like to use the script, a widget is nice, but not the thing u can easily work with. I have changed the mine so he work with Google maps too, click the link and u can drive to me instantly. Maybe it is nice to place ‘Tips’ to =)?

        • https://elie.im Elie Bursztein

          Hey, I am going to work on the script for sure. I just don’t know when. Having a better interaction with Google map is a very good idea. Thanks for the suggestion

  • Pingback: How to display your latest foursquare check-in details on your blog | From Information to Intelligence

  • http://www.sgclark.com/ Stephen Clark

    This is great.  I’ve been looking for something like this for FSQ.  There is WP-Walla for Gowalla but nothing like this for FSQ until now.  

    I think having a widget and the raw PHP code would be ideal so to offer folks the most flexibility.  And adding how to list the last “n” check ins would be great!!  I also think putting the right CSS around each element will help folks customize the style accordingly.  I was hacking around with the code here:  www.sgclark.com/foursquare I commented out the map and just listed the icon, venue and address.  

    Looking forward to the next update!

    • Jerry de Wit

      Hi, that looks nice to ^^ Maybe Elie can make one huge super script about it ghehe ^^

      I will refer to this blog 2, its amazing =D

    • https://elie.im Elie Bursztein

      Hi Stephen,

      Having a better CSS is a very good idea although how to present the data is not directly embedded into the lib. I will try to think of a nice example for this.

      I like the way you display your last place it is very slick :)

      I will try to find sometime this weekend (or next week) to do the plugin.

      Thanks for your support :)

  • http://www.sgclark.com/ Stephen Clark

    Hi:  Not sure if something happened with this script or the API from FSQ but the site I set up below (sgclark.com/foursquare) is not pulling any data from FSQ.  I tried re-setting the API key but still nothing happened. Not sure if anyone else is experiencing this.

    - sgc

    • https://elie.im Elie Bursztein

      Hi Stephen,

      It seems to works now, so I wonder if you didn’t hit the API requests limit. Maybe I should add a builtin caching mechanism so the API is not called to often. How many hit do you have by hour ?

  • BS

    FWIW https://elie.im/foursquare is coming back as Not Found, so I can not generate an OAUTH token. 

    • https://elie.im Elie Bursztein

      Hi,

      Thanks for letting me know. When I upgraded my site code, I forgot to add the link back. It is back to normal. Sorry for the downtime

  • Dennis Vieira

    The other day the foursquare site was having issues and it took my homepage down. Instead of not displaying the widget area that i am pulling the data into, my homepage wouldn’t load and displayed a grey page with an authentication error message. I wish i would have screen grabbed it as I forgot the verbiage. Has anyone else experienced this issue?

    • https://elie.im Elie Bursztein

      I think it is because you are exceeding the number of requested allowed by hour.

      • Dennis Vieira

        Hi Elie,

        My analytics definitely don’t match Foursquare’s max api call at 500 per hour ; )
        If you look at their status page, http://status.foursquare.com/, you will see that the site and app go down quite often. Every time it does, it replaces my homepage with a php error. Is there a way to check if there is a connection, and if there is not, bypass the rest of the code so my homepage can still load?
        Thanks in advance!

  • http://www.facebook.com/demircancelebi Demircan Çelebi

    Hi. First of all, thanks for this. It is awesome work. I am just wondering, is there any way to show last checkins, I mean more than one. Last 2, last 3, last 5. Something like that? 

    • https://elie.im Elie Bursztein

      Hey,
      Yes it is possible. I will write a short post showing you how to do this.

  • Noah

    This is great, thanks for providing it. Do you have suggestions for the best way to display additional checkins? Currently I have it working by changing $rawData in the library to public instead of private and accessing them there — there’s probably a better way, though.

  • Guest

    i try to get this running on a cargocollective template…since i dont have the folder structure to put the .php file into i am trying to outsource the .php

    in that case i put it like this?

    require_once ‘www.example.com/foursquare.php’;
    i am a total noob with this…so, thanks!

  • http://babakfakhamzadeh.com MastaBaba

    Thanks! Your example works great.

    However, is it not the case that because your class doesn’t take the checkin id ($position) as a parameter when the class is constructed, that the only checkin you can get through this class is the most recent one? 

  • http://twitter.com/rolle Roni Laukkarinen

    Hi! It did work since couple of days ago when it stopped working. It prints out empty data. All seem to be OK but can’t see any check ins. Any clue?