Welcome Guest, Not a member yet? Register   Sign In
Getting CodeIngoter to play nicely with facebook
#1

[eluser]coldclimate[/eluser]
Phew - after 2 weeks of solid coding we've replatformed our facebook application from a big script to CI (we've used it on lots of other projects) and relaunched.

Along the way we've had to find lots of little hacks, odd code techniques etc to play nciely with FB (vars in URLs breaking things, using the FB library etc). I wondered if there was any interest frmo anybody here who's doing simalar things if I write up all the tweaks etc we've made to make it smoother for other to get started *it was a bit of a steep learning curve)?

oli

NB: App is at http://j.mp/FBwishlist if anybody fancies a look. No doubt this week will uncover a few screaming bugs Smile
#2

[eluser]philm[/eluser]
Maybe write it all up and post it on the CI wiki?

http://codeigniter.com/wiki/Tutorials/

Smile
#3

[eluser]coldclimate[/eluser]
@phillm Good idea - just as soon as the post go live crazyness settles down I'll write up.
#4

[eluser]gh0st[/eluser]
Please update this forum thread when you create this wiki to advertise its creation. A lot of users, including myself, want to get involved with Facebook programming with CodeIgniter and I'm sure your wiki will help!
#5

[eluser]coldclimate[/eluser]
Will do - sorry it's taking me ages!
#6

[eluser]nealumney[/eluser]
I'd love to hear all about how this works, including where to get the necesary facebook code bits
#7

[eluser]Unknown[/eluser]
I'm eagerly waiting too
#8

[eluser]Kumar Chetan sharma[/eluser]
Well, I am also planing to rewrite a FB app I wrote using core PHP. Waiting for you to echo some long text :-)

Edited, there was a typo Tongue
#9

[eluser]coldclimate[/eluser]
Right - I'm on in. This afternoon I will do nothing but write Smile
#10

[eluser]coldclimate[/eluser]
We're also running against all the old Facebook API, not the new Open Graph stuff that was kicked off at F8. We know we're going to have to rip out the guts of all the FB API call bits in a few weeks, but I'll update that when we do (and get it working).

Huge props to Elliot Haugin for his library, that was a massive help.

A few things up front, which might affect how you build. We're working on an FBML based facebook application (not iFrame), and it's an actual FB app, not a separate website using Facebook Connect. All of this should also apply to iframe base FB apps too, but I thought I'd better be clear. Facebook Connect is a different beast, and I've not played with it yet.

If this is your first attempt at building a FB app, have a go with a single file of vanilla code to make Hello World first rather than inside a code framework - much easier to get the hang of what a Canvas URL is etc etc without having to worry about whether or not CI is getting in the way or it's your FB set up.

We're running a base install of CI 1.7.2, and the first thing we did was install Elliot Haugin's Facebook Connect library (http://www.haughin.com/code/facebook/). We followed his instructions to install the library and set up the config in /applicaitons/config/facebook.php. The bit about xd_receiver.htm is not relevant if you're doing a FB app, rather than a Facebook Connect powered site, but do it anyway.

At this point we battled with FB for a little whilst trying to get the examples to work and failing miserably. There were a few things we needed to do to make it work, namely replace the php wrapper, and add a bit of config to handle getVars.

Call the library correctly in the controller:
In out constructor we call
Code:
$this->load->library('facebook_connect');
We could also do this inside the index function but we needed to have the Facebook User's ID available in the constructor, so did it early.

When we want to access the library we then do it like this..

Code:
//(to get the user's Facebook ID)
$this->facebook_connect->user['uid']  

//(to find if this application has permission to access the user's email address)
$this->facebook_connect->client->users_hasAppPermission("email");  

// to run some FQl to get an email address
$this->facebook_connect->client->fql_query("SELECT email FROM user WHERE uid=12345678");

The PHP Wrapper:
The Facebook PHP wrapper used to change reasonably frequently, and to make things that little bit harder wasn't version managed, so I don't know what version we're currently (or ever have been) using.

When Elliot's examples didn't work (because the API had changed and thus the PHP wrapper was out of date), we tried dropping the latest facebook.php and facebookapi_php5_restlib.php into /application/libraries/facebook-client and things got a lot better. We just downloaded the latest ones from http://wiki.developers.facebook.com/index.php/PHP though these will now not be available because of the Open Graph updates. This will be easier in the future because the new PHP wrapper is version managed over on GitHub

$_GET:
FB throws you lots of stuff in the URL, and whilst we'd love to make use of it (track referrers etc) we had to stop it to make CI play nicely. FB will stuck things like ?id=2579980995&ref=ts on the end of your URLS, and then CI will 404.

To ditch all the information being passed by get we added a hook. Basically every request to the app now gets it's $_GET ditched. Eventually we'll handle this more gracefully. In /application/hooks we created Querystring.php with the following content
Code:
<?php
class Querystring  {
    public function rewrite () {
        $_GET = array();
    }
}
?>

An at this point, we were up and running. We need to construct purely directory based URLs, such as http://apps.facebook.com/wishli-st/wishl...1071971681 and not http://apps.facebook.com/wishli-st/wishl...1071971681 but thats just good URL structures anyway right?




Theme © iAndrew 2016 - Forum software by © MyBB