CodeIgniter Forums

Full Version: Codeigniter and Foursquare API
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]404error[/eluser]
I'm working on a project using Codeigniter and the Foursquare API. I found Stephen Young's Foursquare API Class https://github.com/stephenyoung/php-foursquare listed under libraries for PHP in the Foursquare Docs.

Before I introduced Codeigniter to Stephen's Foursquare Class I played around with the example that Stephen provided. I was making different requests and everything was working beautifully.

I was able to retrieve my personal information from Foursquare:

Code:
<?php require_once("FoursquareAPI.class.php");

// This file is intended to be used as your redirect_uri for the client on Foursquare

// Set your client key and secret

$client_key = "client_key_from_foursquare";
$client_secret = "client_secret_from_foursquare";
$redirect_uri = "http://example.com/callback";

// Load the Foursquare API library
$foursquare = new FoursquareAPI($client_key,$client_secret);

// If the link has been clicked, and we have a supplied code, use it to request a token
if(array_key_exists("code",$_GET))
{
$token = $foursquare->GetToken($_GET['code'],$redirect_uri);
}

// If we have not received a token, display the link for Foursquare webauth

if(!isset($token))
{
echo "<a href='".$foursquare-&gt;AuthenticationLink($redirect_uri)."'>Connect to Foursquare</a>";
// Return Current users first name
}else{
$request = $foursquare->GetPrivate('users/self');
$details = json_decode($request);
$u = $details->response->user;
echo "Hello ".$u->firstName;
}
?&gt;

This was working, so I decided to make this a Codeigniter Library.

I moved the Foursquare API class to the libraries folder, created a config file for it that loads the $client_key, $client_secret, and $redirect_uri.

I created a controller:

Code:
&lt;?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Welcome extends CI_Controller {

function __construct(){
parent::__construct();
//load the foursquare_api library
$this->load->library('foursquare_api');
}

public function index()
{
//create a link to connect with Foursquare
$data['auth_link'] = $this->foursquare_api->AuthenticationLink();

$this->load->view('home', $data);
}

public function back()
{
//GET the code in the url  
$code = $this->input->get('code', TRUE);

//get and store token
$data['token'] = $this->foursquare_api->GetToken($code);

//Get current user information from Foursquare
$data['request'] = json_decode($this->foursquare_api->GetPrivate('users/self'));

$this->load->view('welcome_to', $data);
}

}

/* End of file welcome.php *//* Location: ./application/controllers/welcome.php */?&gt;

In my view file all am doing is using echo to print the $token to the page, which is working correctly. I am able to generate a token.

When I make a request to get the current user's information I get back nothing. I'm obviously doing something wrong, I just dont know what it is I'm doing wrong.

Can anyone offer some insight or point me in the right direction?