Skip to content

Instantly share code, notes, and snippets.

@furious
Last active December 25, 2015 12:19
Show Gist options
  • Save furious/6975782 to your computer and use it in GitHub Desktop.
Save furious/6975782 to your computer and use it in GitHub Desktop.
Simple Foursquare Class: Check-In v1.1
<?
define("FS_CLIENTID", "CLIENT_ID");
define("FS_CLIENTSECRET", "CLIENT_SECRET");
define("FS_REDIRECTURI", "http://localhost");
define("FS_ACCESSTOKEN", "ACCESSTOKEN");
// https://foursquare.com/oauth2/authenticate?client_id=CLIENT_ID&response_type=token&redirect_uri=REDIRECT_URI
class FS_API
{
private $curl;
var $client_id = FS_CLIENTID;
var $client_secret = FS_CLIENTSECRET;
var $redirect_uri = FS_REDIRECTURI;
var $user_code;
private $access_token;
function __construct($access_token=false){
$this->curl = curl_init();
$this->access_token = $access_token ? $access_token : $this->get_token();
}
function __destruct(){
curl_close($this->curl);
}
/*
* checkin() - Make a check-in anywhere
* data array check-in informations
* - 'venue' array venue data from fetch_venue() function
* - 'private' bool show or hide check-in to your followers
* - 'shout' string set a message for the check-in
*
* return array informations about the check-in made
*/
public function checkin($data){
# make checkin
$request = array(
'venueId' => $data['venue']['id'],
'll' => "{$data[venue][location][lat]},{$data[venue][location][lng]}",
'shout' => $data['shout'] ? $data['shout'] : '',
'broadcast' => $data['private'] ? 'private' : 'public',
'oauth_token' => $this->access_token,
'v' => date("Ym01")
);
return $this->request("https://api.foursquare.com/v2/checkins/add", true, $request);
}
/*
* fetch_venue() - Get venue information by it's ID or URL
* id string alphanumeric ID or the venue URL
*
* return array all the data from venue, additionally default settings for check-in
*/
public function fetch_venue($id){
if(substr($id, 0, 4) == "http")
$id = preg_match('/.*\/([[:alnum:]]+)$/', $id, $match) ? $match[1] : $id;
$response = $this->request("https://api.foursquare.com/v2/venues/$id?client_id={$this->client_id}&oauth_token={$this->access_token}&v=" . date("Ym01"), true);
if($response['meta']['code'] == 200){
$data = array(
'venue' => $response['response']['venue'],
'private' => true,
'shout' => false
);
return $data;
} else {
return false;
}
}
/*
* get_token() - Fetch token using CODE
*
* return bool successful request?
*/
private function get_token(){
$request = http_build_query(array(
'client_id' => $this->client_id,
'client_secret' => $this->client_secret,
'code' => $this->user_code,
'grant_type' => 'authorization_code',
'redirect_uri' => $this->redirect_uri
));
$access_token = $this->request("https://foursquare.com/oauth2/access_token?" . $request, true);
return !isset($access_token['error']) ? $access_token['access_token'] : false;
}
/*
* request() - All http requests are made by this function
* url string the link to request
* json bool parse the response with json_decode()
* post array make a post request with data
*
* return mixed return an array or string based in json parameter
*/
private function request($url, $json=false, $post=false){
curl_setopt_array($this->curl, array(
CURLOPT_URL => $url,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_USERAGENT => "FURiOUS 1.0"
));
if(is_array($post)){
curl_setopt_array($this->curl, array(
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => http_build_query($post)
));
}
$return = curl_exec($this->curl);
return $json ? json_decode($return, true) : $return;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment