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

555236_10150833589426073_1299829205_n

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 :)

Elie Bursztein is a researcher at Google where he works on fixing Internet security and privacy problems. This is my personal blog.
Share me:
  • 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

      • http://twitter.com/noverotajs Mr. Jones

         forbiden

  • 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.

      • http://www.facebook.com/edgar.moskopp Edgar Moskopp

         

        Dear Elie

         

        I am using
        your Method and it works very nice!! Have you already written the post on how
        to show the last 5 checkins or – if not – do you still plan on doing so?

         

        Best
        regards from Austria

        Edgar

  • 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?

  • http://twitter.com/parthibalo parthib alo

    links doesn’t work https://elie.im/foursquare ! I got this error   ”
    Fatal error: Class ‘foursquare’ not found in C:xampphtdocsfoursquaremapcheckin.php on line 16″ 

    sorry noob here can you please tell me where I did wrong ?

    thanks

    • https://elie.im Elie Bursztein

      Sorry. It was my fault. it is fixed.

  • Luke Lau

    https://elie.im/foursquare seems to be down :(

    • http://www.djnightlife.com DJ NightLife

      +1

      • https://elie.im Elie Bursztein

        sorry about this. Updating the site broke the link. It is fixed. Thanks for letting me know.

  • Robert Diaz

    Thank you Elie! This is very useful and was clear enough for me to follow. I am currently testing it on a WordPress-based site, so I can post a link once it is set up in final form. The only adjustment I made to it was that I used the check-in venue variables to generate a google maps page link that I linked to the map image. You’re the man!!

    • http://ExpertVagabond.com/ Matthew Karsten

      How did you do this? I would like to link the city & country to a google maps page…

      • Robert Diaz

         Hi Matthew,

        Just to preface my response, I’m not a programmer, so maybe there’s a shorter/more efficient way to code this. Anyway, try this code to link your latest check-in map to a Google map of the city and country of the latest check-in (this goes in the php file that you will call into your site):
                     
                       
                       
                            <a href="https://maps.google.com/maps?q=venueCity; ?>+venueCountry; ?>&z=16&iwloc=A” target=”_blank”><img style="background-color: #fff; border: 1px solid #ccc; padding: 3px;" src="getMapUrl(260, 245); ?>” />
                       

        Basically, I looked at how Google’s map URLs are formatted. Then, I formatted a link tag plugging in the variables that Elie created in his file.

        The ‘z=16′ refers to the zoom setting of the Google map (higher number zooms in more, lower number zooms out more).

        Let me know if you have questions about anything else and I’ll do my best to help you :)

        • Robert Diaz

          Sorry about that, looks like the comment box formatted the code block I put in my response. I’ve attached a screenshot here of the code snippet. Alternatively, you can e-mail me at diaz.creative-at-gmail-dot-com and I can send you the code in a file or pasted into an e-mail.
           
           
          Best,

          Robert

  • http://www.aluminarium.com/ Leigh

    Worked great for me, thanks for the directions!  http://www.aluminarium.com

  • Teja

    Didnt worked for me.

  • Pingback: Lastest Foursquare checkin on map | Code and Programming

  • http://jerrydewit.nl/ Jerry

    Hi there ^^, maybe a update to switch over to OpenStreetmap? Just like FourSquare did?

  • Robert Diaz

    Hello Elie! Hope all is well. Just curious, FourSquare has been down today and I noticed that my site does not load anything after the FourSquare widget (basically looks like the site has crashed). I quickly commented out the call to the script and replaced it with a message so the rest of the site would display.

    Any ideas on how to set up a test to first see if FourSquare is running? If it is, we can continue to run the rest of the script. If not, we can output a message saying that it is down at the moment.

Popular blog posts
Latest social News
What Your Facebook Profile Photo Says About You - http://t.co/THHqZQxFem#fb#twitter#psych#smo#seo#privacy
1 day ago
Emotion color guide. Awesome ! What is your color? :)#design#web#art#ux http://t.co/bndVZysO5t
1 day ago
When a porn site masquerades as the Apple App Store - http://t.co/x3r0UKGUlX#ios#apple#security#seo#mobile
'Financial Times' Website and Twitter Accounts Hacked - http://t.co/asOWR3Hdi5#security#privacy #syrianelectronicarmy#wsj
Twitter ‘Hate Map’ shows where racist, homophobic, and offensive tweets originate - http://t.co/4cJUSmMZEO#freedom#twitter#web