Implementing foursquare API With Ease

Implementing foursquare API With Ease

Implementing foursquare API With Ease

foursquare is one of the best location-based social networking website. Before Facebook’s check-in it was on the top of the list. foursquare also have api like facebook. They used OAuth2.0 which i just love. Because of this, implementing foursquare is very easy. The best thing is if you obtain the access_token once you don’t need to get it again unless until user deletes the app form his/her profile. I implement foursquare api couple of month back on

Before starting we need to register our app. Every app is assigned to url. So first register an app here. the “Callback URL” is where foursquare will redirect after user authorize the app. Now you have “Client ID”, “Client Secret” and “Callback URL”.

Now we send our user to authorize our app. So we need to build a url to do that. The url is

Here YOUR_CLIENT_ID=”Client ID”, YOUR_REGISTERED_REDIRECT_URI=”Callback URL”. Now if the user authorize the app, foursquare will redirect the user with a code. We will use the code to get the access_token.

Now with the code will get access_token from. The url looks like this

Here YOUR_CLIENT_SECRET=”Client Secret” , CODE=is the code get from previous url. Now is everything goes as it should be you will get a json string. Something like this

{ access_token: ACCESS_TOKEN }

So that’s it. We can now get user different resources using this access_token.

Now i will show you how you can do this in php. See it in action here. You can also download the files from here.

At first we intialize “Client ID”, “Client Secret” and “Callback URL” according to our app.


In index.php a image is linked with $authorize_url url. When user clicks it redirect user to foursquare. when he/she clcik allow, it will redirect to callback.php. Here we add the code to $access_token_url and file_get_contents it then we decode the json. we get the access_token.

header('Location: show_details.php');

In show_details.php, we get users badges and chekins with file_get_contents. But to add check-in we can not use it. we use curl caue we have to use POST method.

	$url = '';
	$fields = array(
				'venue'=>'Lindquist Field',
	foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }

	$ch = curl_init();
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

You see a whole list of apis here.


About Zakir Hyder

This entry was written by .

23. July 2011 by Zakir Hyder
Categories: foursquare, Social Networking Application Development | Tags: , , , , | Comments


  1. […] tutorials, and libraries (including the Epi-async library) and the best one I have found is this: This is what my code is based off […]