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 http://explorer.io/

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

https://foursquare.com/oauth2/authenticate?
client_id=YOUR_CLIENT_ID
&response_type=code
&redirect_uri=YOUR_REGISTERED_REDIRECT_URI

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

https://foursquare.com/oauth2/access_token
?client_id=YOUR_CLIENT_ID
&client_secret=YOUR_CLIENT_SECRET
&grant_type=authorization_code
&redirect_uri=YOUR_REGISTERED_REDIRECT_URI
&code=CODE

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.

session_start();
$callback_url="http://jambura.com/4qs/callback.php";
$client_id="1T0BIXJBFHRVB3WZSH3I1ZT4SYH1LTABNG2W2SC0VBHQ1BIP";
$client_secret="EUJCMWNGMIWM4U44NSTB5YCS4RWDMYBR5K0GM3YRRFH0SJ0R";
$authorize_url="https://foursquare.com/oauth2/authorize?client_id={$client_id}&response_type=code&redirect_uri={$callback_url}";
$access_token_url="https://foursquare.com/oauth2/access_token?client_id={$client_id}&client_secret={$client_secret}&grant_type=authorization_code&redirect_uri={$callback_url}&code=";

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.

include('config.php');
$access_token_url=$access_token_url.$_GET['code'];
$access_token=json_decode(file_get_contents("{$access_token_url}"),true);
$_SESSION['access_token']=$access_token['access_token'];
header('Location: show_details.php');
exit;

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 = 'https://api.foursquare.com/v2/checkins/add';
	$fields = array(
				'venueId'=>'4b68cf11f964a5200b8e2be3',
				'venue'=>'Lindquist Field',
				'shout'=>'Jambura',
				'broadcast'=>'public',
				'll'=>'41.2243469,-111.97555',
				'oauth_token'=>$_SESSION['access_token']
			);
	foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
	rtrim($fields_string,'&');

	$ch = curl_init();
	curl_setopt($ch,CURLOPT_URL,$url);
	curl_setopt($ch,CURLOPT_POST,count($fields));
	curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_exec($ch);
	curl_close($ch);

.
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

Comments

  1. […] tutorials, and libraries (including the Epi-async library) and the best one I have found is this: http://blog.jambura.com/2011/07/23/implementing-foursquare-api-with-ease/. This is what my code is based off […]